osMemory.h 3.0 KB
Newer Older
S
Shengliang Guan 已提交
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
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TDENGINE_OS_MEMORY_H
#define TDENGINE_OS_MEMORY_H

#include "osString.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
  TAOS_ALLOC_MODE_DEFAULT = 0,
  TAOS_ALLOC_MODE_RANDOM_FAIL = 1,
  TAOS_ALLOC_MODE_DETECT_LEAK = 2
} ETaosMemoryAllocMode;

void   taosSetAllocMode(int mode, const char *path, bool autoDump);
void   taosDumpMemoryLeak();

S
TD-1848  
Shengliang Guan 已提交
34
// used in tsdb module
S
Shengliang Guan 已提交
35 36 37 38 39 40 41
void * taosTMalloc(size_t size);
void * taosTCalloc(size_t nmemb, size_t size);
void * taosTRealloc(void *ptr, size_t size);
void   taosTZfree(void *ptr);
size_t taosTSizeof(void *ptr);
void   taosTMemset(void *ptr, int c);

S
TD-1848  
Shengliang Guan 已提交
42 43 44 45 46 47 48 49
// used in other module
#define tmalloc(size) malloc(size)
#define tcalloc(num, size) calloc(num, size)
#define trealloc(ptr, size) realloc(ptr, size)
#define tstrdup(str) taosStrdupImp(str)
#define tstrndup(str, size) taosStrndupImp(str, size)
#define tgetline(lineptr, n, stream) taosGetlineImp(lineptr, n, stream)
#define tfree(x)         \
S
Shengliang Guan 已提交
50 51 52 53 54 55 56
  do {                   \
    if (x) {             \
      free((void *)(x)); \
      x = 0;             \
    }                    \
  } while (0);

S
Shengliang Guan 已提交
57 58
#ifdef TAOS_MEM_CHECK
  #ifdef TAOS_MEM_CHECK_TEST
S
TD-1848  
Shengliang Guan 已提交
59 60 61 62 63 64 65 66 67 68
    void *  taosMallocMem(size_t size, const char *file, uint32_t line);
    void *  taosCallocMem(size_t num, size_t size, const char *file, uint32_t line);
    void *  taosReallocMem(void *ptr, size_t size, const char *file, uint32_t line);
    void    taosFreeMem(void *ptr, const char *file, uint32_t line);
    char *  taosStrdupMem(const char *str, const char *file, uint32_t line);
    char *  taosStrndupMem(const char *str, size_t size, const char *file, uint32_t line);
    ssize_t taosGetlineMem(char **lineptr, size_t *n, FILE *stream, const char *file, uint32_t line);
    #undef tmalloc
    #undef tcalloc
    #undef trealloc
S
TD-1848  
Shengliang Guan 已提交
69
    #undef tfree
S
TD-1848  
Shengliang Guan 已提交
70 71 72 73 74 75 76 77 78 79 80
    #define tmalloc(size) taosMallocMem(size, __FILE__, __LINE__)
    #define tcalloc(num, size) taosCallocMem(num, size, __FILE__, __LINE__)
    #define trealloc(ptr, size) taosReallocMem(ptr, size, __FILE__, __LINE__)
    #define tfree(ptr) taosFreeMem(ptr, __FILE__, __LINE__) 
    
    // #undef tstrdup
    // #undef tstrndup
    // #undef tgetline
    // #define taosStrdup(str) taos_strdup(str, __FILE__, __LINE__)
    // #define taosStrndup(str, size) taos_strndup(str, size, __FILE__, __LINE__)
    // #define tgetline(lineptr, n, stream) taos_getline(lineptr, n, stream, __FILE__, __LINE__)
S
Shengliang Guan 已提交
81
  #endif  
S
Shengliang Guan 已提交
82 83 84 85 86 87 88
#endif 

#ifdef __cplusplus
}
#endif

#endif