18b52a31eSHanoh Haim#ifndef _BASIC_UTILS_H
28b52a31eSHanoh Haim#define _BASIC_UTILS_H
38b52a31eSHanoh Haim
48b52a31eSHanoh Haim/*
58b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
68b52a31eSHanoh Haim
78b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
88b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
98b52a31eSHanoh HaimYou may obtain a copy of the License at
108b52a31eSHanoh Haim
118b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
128b52a31eSHanoh Haim
138b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
148b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
158b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
168b52a31eSHanoh HaimSee the License for the specific language governing permissions and
178b52a31eSHanoh Haimlimitations under the License.
188b52a31eSHanoh Haim*/
198b52a31eSHanoh Haim
208b52a31eSHanoh Haim#include "c_common.h"
218b52a31eSHanoh Haim#include <stdio.h>
228b52a31eSHanoh Haim#include <string>
238b52a31eSHanoh Haim
24eae78d43SHanoh Haim/**
25eae78d43SHanoh Haim * return  true if number of log2
26eae78d43SHanoh Haim *
27eae78d43SHanoh Haim * @param num
28eae78d43SHanoh Haim *
29eae78d43SHanoh Haim * @return
30eae78d43SHanoh Haim */
31eae78d43SHanoh Haiminline bool utl_islog2(uint32_t num){
32eae78d43SHanoh Haim    uint32_t mask=1;
33eae78d43SHanoh Haim    int i;
34eae78d43SHanoh Haim    for (i=0; i<31; i++) {
35eae78d43SHanoh Haim        if (mask == num) {
36eae78d43SHanoh Haim            return (true);
37eae78d43SHanoh Haim        }
38eae78d43SHanoh Haim        if (mask > num) {
39eae78d43SHanoh Haim            return(false);
40eae78d43SHanoh Haim        }
41eae78d43SHanoh Haim        mask=mask<<1;
42eae78d43SHanoh Haim    }
43eae78d43SHanoh Haim    return (false);
44eae78d43SHanoh Haim}
45eae78d43SHanoh Haim
46eae78d43SHanoh Haiminline uint32_t utl_log2_shift(uint32_t num){
47eae78d43SHanoh Haim    uint32_t mask=1;
48eae78d43SHanoh Haim    int i;
49eae78d43SHanoh Haim    for (i=0; i<31; i++) {
50eae78d43SHanoh Haim        if (mask == num) {
51eae78d43SHanoh Haim            return ((uint32_t)i);
52eae78d43SHanoh Haim        }
53eae78d43SHanoh Haim        if (mask > num) {
54eae78d43SHanoh Haim            return(false);
55eae78d43SHanoh Haim        }
56eae78d43SHanoh Haim        mask=mask<<1;
57eae78d43SHanoh Haim    }
58eae78d43SHanoh Haim    assert(0);
59eae78d43SHanoh Haim    return (-1);
60eae78d43SHanoh Haim}
61eae78d43SHanoh Haim
62eae78d43SHanoh Haim
63eae78d43SHanoh Haim/**
64eae78d43SHanoh Haim * return mask for log2 number
65eae78d43SHanoh Haim *
66eae78d43SHanoh Haim * @param num
67eae78d43SHanoh Haim *
68eae78d43SHanoh Haim * @return
69eae78d43SHanoh Haim */
70eae78d43SHanoh Haiminline uint32_t utl_mask_log2(uint32_t num){
71eae78d43SHanoh Haim    return (num-1);
72eae78d43SHanoh Haim}
73eae78d43SHanoh Haim
74eae78d43SHanoh Haim
758b52a31eSHanoh Haim/**
768b52a31eSHanoh Haim * the round must be power 2 e.g 2,4,8...
778b52a31eSHanoh Haim *
788b52a31eSHanoh Haim * @param num
798b52a31eSHanoh Haim * @param round
808b52a31eSHanoh Haim * @return
818b52a31eSHanoh Haim */
828b52a31eSHanoh Haiminline uint utl_align_up(uint num,uint round){
838b52a31eSHanoh Haim    if ((num & ((round-1)) )==0) {
848b52a31eSHanoh Haim        //the number align
858b52a31eSHanoh Haim        return(num);
868b52a31eSHanoh Haim    }
878b52a31eSHanoh Haim    return( (num+round) & (~(round-1)) );
888b52a31eSHanoh Haim}
898b52a31eSHanoh Haim
908b52a31eSHanoh Haiminline uint utl_align_down(uint num,uint round){
918b52a31eSHanoh Haim    return( (num) & (~(round-1)) );
928b52a31eSHanoh Haim}
938b52a31eSHanoh Haim
94567fe4bcSHanoh Haimvoid utl_k12_pkt_format(FILE* fp,void  * src,  unsigned int size) ;
958b52a31eSHanoh Haim
968b52a31eSHanoh Haimvoid utl_DumpBuffer(FILE* fp,void  * src,  unsigned int size,int offset=0);
978b52a31eSHanoh Haim
988b52a31eSHanoh Haim
998b52a31eSHanoh Haim
1008b52a31eSHanoh Haim#define SHOW_BUFFER_ADDR_EN     1
1018b52a31eSHanoh Haim#define SHOW_BUFFER_ADDR        2
1028b52a31eSHanoh Haim#define SHOW_BUFFER_CHAR        4
1038b52a31eSHanoh Haim
1048b52a31eSHanoh Haim#define SHOW_BUFFER_ALL (SHOW_BUFFER_ADDR_EN|SHOW_BUFFER_ADDR|SHOW_BUFFER_CHAR)
1058b52a31eSHanoh Haim
1068b52a31eSHanoh Haimvoid utl_DumpBuffer2(FILE* fd,
1078b52a31eSHanoh Haim                     void  * src,
1088b52a31eSHanoh Haim                     unsigned int size, //buffer size
1098b52a31eSHanoh Haim                     unsigned int width ,
1108b52a31eSHanoh Haim                     unsigned int width_line ,
1118b52a31eSHanoh Haim                     unsigned int mask);
1128b52a31eSHanoh Haim
1138b52a31eSHanoh Haim
1148b52a31eSHanoh Haim
1158b52a31eSHanoh Haim#undef min
1168b52a31eSHanoh Haim#undef max
1178b52a31eSHanoh Haim
1188b52a31eSHanoh Haimtemplate <class T>
1198b52a31eSHanoh Haiminline const T& utl_min(const T& a, const T& b) {
1208b52a31eSHanoh Haim  return b < a ? b : a;
1218b52a31eSHanoh Haim}
1228b52a31eSHanoh Haim
1238b52a31eSHanoh Haimtemplate <class T>
1248b52a31eSHanoh Haiminline const T& utl_max(const T& a, const T& b) {
1258b52a31eSHanoh Haim  return  a < b ? b : a;
1268b52a31eSHanoh Haim}
1278b52a31eSHanoh Haim
1288b52a31eSHanoh Haimtemplate <class T>
1298b52a31eSHanoh Haiminline void utl_swap(T& a, T& b) {
1308b52a31eSHanoh Haim  T tmp = a;
1318b52a31eSHanoh Haim  a = b;
1328b52a31eSHanoh Haim  b = tmp;
1338b52a31eSHanoh Haim}
1348b52a31eSHanoh Haim
1358b52a31eSHanoh Haim
1368b52a31eSHanoh Haimbool utl_is_file_exists (const std::string& name) ;
1378b52a31eSHanoh Haim
13821fe2befSimaromvoid utl_macaddr_to_str(const uint8_t *macaddr, std::string &output);
139c9ec3b81SIdo Barneastd::string utl_macaddr_to_str(const uint8_t *macaddr);
1408b52a31eSHanoh Haim
1416e1919c3Simarombool utl_str_to_macaddr(const std::string &s, uint8_t *mac);
142ab28fcccSimarom
1434eacb570Simaromstd::string utl_generate_random_str(unsigned int &seed, int len);
1444eacb570Simarom
1455f846485Simarom/**
1465f846485Simarom * define the coredump size
1475f846485Simarom * allowed when crashing
1485f846485Simarom *
1495f846485Simarom * @param size - -1 means unlimited
1505f846485Simarom * @param map_huge_pages - should the core map the huge TLB
1515f846485Simarom *                       pages
1525f846485Simarom */
1535f846485Simaromvoid utl_set_coredump_size(long size, bool map_huge_pages = false);
1545f846485Simarom
155c420d1fdSimarombool           utl_ipv4_to_uint32(const char *ipv4_str, uint32_t &ipv4_num);
156ab28fcccSimaromstd::string    utl_uint32_to_ipv4(uint32_t ipv4_addr);
157ab28fcccSimarom
1588b52a31eSHanoh Haim#endif
1598b52a31eSHanoh Haim
1608b52a31eSHanoh Haim
161