TD-100
This commit is contained in:
parent
f15535afc3
commit
a85c04c0fb
|
@ -178,6 +178,13 @@ void taosDumpMemoryLeak();
|
|||
void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
|
||||
int (*compar)(const void *, const void *), int flags);
|
||||
|
||||
void * tmalloc(size_t size);
|
||||
void * tcalloc(size_t nmemb, size_t size);
|
||||
size_t tsizeof(void *ptr);
|
||||
void tmemset(void *ptr, int c);
|
||||
void * trealloc(void *ptr, size_t size);
|
||||
void tzfree(void *ptr);
|
||||
|
||||
#ifdef TAOS_MEM_CHECK
|
||||
|
||||
void * taos_malloc(size_t size, const char *file, uint32_t line);
|
||||
|
|
|
@ -689,3 +689,42 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
|
|||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *tmalloc(size_t size) {
|
||||
if (size <= 0) return NULL;
|
||||
|
||||
void *ret = malloc(size + sizeof(size_t));
|
||||
if (ret == NULL) return NULL;
|
||||
|
||||
*(size_t *)ret = size;
|
||||
|
||||
return (void *)((char *)ret + sizeof(size_t));
|
||||
}
|
||||
|
||||
void *tcalloc(size_t nmemb, size_t size) {
|
||||
size_t tsize = nmemb * size;
|
||||
void * ret = tmalloc(tsize);
|
||||
if (ret == NULL) return NULL;
|
||||
|
||||
tmemset(ret, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t tsizeof(void *ptr) { return *(size_t *)((char *)ptr - sizeof(size_t)); }
|
||||
|
||||
void tmemset(void *ptr, int c) { memset(ptr, c, tsizeof(ptr)); }
|
||||
|
||||
void * trealloc(void *ptr, size_t size) {
|
||||
if (size <= tsizeof(ptr)) return ptr;
|
||||
|
||||
void * tptr = (void *)((char *)ptr - sizeof(size_t));
|
||||
size_t tsize = size + sizeof(size_t);
|
||||
tptr = realloc(tptr, tsize);
|
||||
if (tptr == NULL) return NULL;
|
||||
|
||||
*(size_t *)tptr = size;
|
||||
|
||||
return (void *)((char *)tptr + sizeof(size_t));
|
||||
}
|
||||
|
||||
void tzfree(void *ptr) { free((void *)((char *)ptr - sizeof(size_t))); }
|
Loading…
Reference in New Issue