LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-19 ec8c799d8bb2ee69b5e6f56201231e8c905edeb1
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
/********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue<guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  cp_common.h
 *    Description:  This head file is for some common definition
 *
 *        Version:  1.0.0(11/13/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "11/13/2012 01:48:01 PM"
 *                 
 ********************************************************************************/
 
#ifndef __CP_COMMON_H
#define __CP_COMMON_H
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
 
#define container_of(ptr, type, member) ({   \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) );})
 
//#define MEM_LEAK_CHECK
 
#if 0
static inline void *_t_malloc(size_t size
#ifdef MEM_LEAK_CHECK
, const char *file, unsigned int line
#endif
        )
{
    void *ptr;
    
    if ((ptr = malloc (size)))
        memset (ptr, 0, size);
#ifdef MEM_LEAK_CHECK
    printf ("MALLOC,0x%p @%s:%u\n", ptr, file, line);
#endif
    return ptr;
}
 
static inline void _t_free(void *ptr
#ifdef MEM_LEAK_CHECK
, const char *file, unsigned int line
#endif
        )
{
#ifdef MEM_LEAK_CHECK
    printf ("FREE,0x%p @%s:%u\n", ptr, file, line);
#endif
    free(ptr);
}
 
#ifdef MEM_LEAK_CHECK
#define t_free(p)    if(p){ _t_free(p, __FILE__, __LINE__); p=NULL; }
#define t_malloc(s)  _t_malloc(s, __FILE__, __LINE__)
#else
#define t_free(p)    if(p){ _t_free(p); p=NULL; }
#define t_malloc(s)  _t_malloc(s)
#endif
 
#endif
 
#endif  /* end of __CP_COMMON_H */