summaryrefslogtreecommitdiffstats
path: root/src/common/basic_utils.h
blob: 36f9db850ab2e395e0496ca63afaf5a1d9f5c09f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef _BASIC_UTILS_H
#define _BASIC_UTILS_H

/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "c_common.h"
#include <stdio.h>
#include <string>

/**
 * the round must be power 2 e.g 2,4,8...
 * 
 * @param num
 * @param round
 * @return 
 */
inline uint utl_align_up(uint num,uint round){
    if ((num & ((round-1)) )==0) {
        //the number align
        return(num);
    }
    return( (num+round) & (~(round-1)) );
}

inline uint utl_align_down(uint num,uint round){
    return( (num) & (~(round-1)) );
}

void utl_k12_pkt_format(FILE* fp,void  * src,  unsigned int size) ;

void utl_DumpBuffer(FILE* fp,void  * src,  unsigned int size,int offset=0);



#define SHOW_BUFFER_ADDR_EN     1
#define SHOW_BUFFER_ADDR        2
#define SHOW_BUFFER_CHAR        4

#define SHOW_BUFFER_ALL (SHOW_BUFFER_ADDR_EN|SHOW_BUFFER_ADDR|SHOW_BUFFER_CHAR)

void utl_DumpBuffer2(FILE* fd,
                     void  * src,  
                     unsigned int size, //buffer size
                     unsigned int width ,
                     unsigned int width_line ,
                     unsigned int mask);



#undef min
#undef max

template <class T>
inline const T& utl_min(const T& a, const T& b) {
  return b < a ? b : a;
}

template <class T>
inline const T& utl_max(const T& a, const T& b) {
  return  a < b ? b : a;
}

template <class T>
inline void utl_swap(T& a, T& b) {
  T tmp = a;
  a = b;
  b = tmp;
}


bool utl_is_file_exists (const std::string& name) ;

void utl_macaddr_to_str(const uint8_t *macaddr, std::string &output);
std::string utl_macaddr_to_str(const uint8_t *macaddr);

bool utl_str_to_macaddr(const std::string &s, uint8_t *mac);

std::string utl_generate_random_str(unsigned int &seed, int len);

/**
 * define the coredump size 
 * allowed when crashing 
 * 
 * @param size - -1 means unlimited
 * @param map_huge_pages - should the core map the huge TLB 
 *                       pages
 */
void utl_set_coredump_size(long size, bool map_huge_pages = false);

bool           utl_ipv4_to_uint32(const char *ipv4_str, uint32_t &ipv4_num);
std::string    utl_uint32_to_ipv4(uint32_t ipv4_addr);
   
#endif