1#ifndef _BASIC_UTILS_H
2#define _BASIC_UTILS_H
3
4/*
5Copyright (c) 2015-2015 Cisco Systems, Inc.
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19
20#include "c_common.h"
21#include <stdio.h>
22#include <string>
23
24/**
25 * return  true if number of log2
26 *
27 * @param num
28 *
29 * @return
30 */
31inline bool utl_islog2(uint32_t num){
32    uint32_t mask=1;
33    int i;
34    for (i=0; i<31; i++) {
35        if (mask == num) {
36            return (true);
37        }
38        if (mask > num) {
39            return(false);
40        }
41        mask=mask<<1;
42    }
43    return (false);
44}
45
46inline uint32_t utl_log2_shift(uint32_t num){
47    uint32_t mask=1;
48    int i;
49    for (i=0; i<31; i++) {
50        if (mask == num) {
51            return ((uint32_t)i);
52        }
53        if (mask > num) {
54            return(false);
55        }
56        mask=mask<<1;
57    }
58    assert(0);
59    return (-1);
60}
61
62
63/**
64 * return mask for log2 number
65 *
66 * @param num
67 *
68 * @return
69 */
70inline uint32_t utl_mask_log2(uint32_t num){
71    return (num-1);
72}
73
74
75/**
76 * the round must be power 2 e.g 2,4,8...
77 *
78 * @param num
79 * @param round
80 * @return
81 */
82inline uint utl_align_up(uint num,uint round){
83    if ((num & ((round-1)) )==0) {
84        //the number align
85        return(num);
86    }
87    return( (num+round) & (~(round-1)) );
88}
89
90inline uint utl_align_down(uint num,uint round){
91    return( (num) & (~(round-1)) );
92}
93
94void utl_k12_pkt_format(FILE* fp,void  * src,  unsigned int size) ;
95
96void utl_DumpBuffer(FILE* fp,void  * src,  unsigned int size,int offset=0);
97
98
99
100#define SHOW_BUFFER_ADDR_EN     1
101#define SHOW_BUFFER_ADDR        2
102#define SHOW_BUFFER_CHAR        4
103
104#define SHOW_BUFFER_ALL (SHOW_BUFFER_ADDR_EN|SHOW_BUFFER_ADDR|SHOW_BUFFER_CHAR)
105
106void utl_DumpBuffer2(FILE* fd,
107                     void  * src,
108                     unsigned int size, //buffer size
109                     unsigned int width ,
110                     unsigned int width_line ,
111                     unsigned int mask);
112
113
114
115#undef min
116#undef max
117
118template <class T>
119inline const T& utl_min(const T& a, const T& b) {
120  return b < a ? b : a;
121}
122
123template <class T>
124inline const T& utl_max(const T& a, const T& b) {
125  return  a < b ? b : a;
126}
127
128template <class T>
129inline void utl_swap(T& a, T& b) {
130  T tmp = a;
131  a = b;
132  b = tmp;
133}
134
135
136bool utl_is_file_exists (const std::string& name) ;
137
138void utl_macaddr_to_str(const uint8_t *macaddr, std::string &output);
139std::string utl_macaddr_to_str(const uint8_t *macaddr);
140
141bool utl_str_to_macaddr(const std::string &s, uint8_t *mac);
142
143std::string utl_generate_random_str(unsigned int &seed, int len);
144
145/**
146 * define the coredump size
147 * allowed when crashing
148 *
149 * @param size - -1 means unlimited
150 * @param map_huge_pages - should the core map the huge TLB
151 *                       pages
152 */
153void utl_set_coredump_size(long size, bool map_huge_pages = false);
154
155bool           utl_ipv4_to_uint32(const char *ipv4_str, uint32_t &ipv4_num);
156std::string    utl_uint32_to_ipv4(uint32_t ipv4_addr);
157
158#endif
159
160
161