static void *mempool_calloc(const char *filename, int line, size_t nmemb, size_t size) { void *ptr; ptr = acl_allocator_membuf_alloc(filename, line, __var_allocator, nmemb * size); memset(ptr, 0, nmemb * size); return (ptr); }
static void *mempool_memdup(const char *filename, int line, const void *ptr, size_t len) { void *buf; buf = acl_allocator_membuf_alloc(filename, line, __var_allocator, len + 1); memcpy(buf, ptr, len); return (buf); }
static char *mempool_strdup(const char *filename, int line, const char *str) { char *ptr; size_t size = strlen(str) + 1; ptr = acl_allocator_membuf_alloc(filename, line, __var_allocator, size); strcpy(ptr, str); return (ptr); }
static void *mempool_malloc_with_mutex(const char *filename, int line, size_t size) { void *ptr; MUTEX_LOCK; ptr = acl_allocator_membuf_alloc(filename, line, __var_allocator, size); MUTEX_UNLOCK; return (ptr); }
static void *mempool_memdup_with_mutex(const char *filename, int line, const void *ptr, size_t len) { void *buf; MUTEX_LOCK; buf = acl_allocator_membuf_alloc(filename, line, __var_allocator, len + 1); MUTEX_UNLOCK; memcpy(buf, ptr, len); return (buf); }
static char *mempool_strdup_with_mutex(const char *filename, int line, const char *str) { char *ptr; size_t size = strlen(str) + 1; MUTEX_LOCK; ptr = acl_allocator_membuf_alloc(filename, line, __var_allocator, size); MUTEX_UNLOCK; strcpy(ptr, str); return (ptr); }
static char *mempool_strndup(const char *filename, int line, const char *str, size_t len) { char *ptr; const char *cp; if ((cp = memchr(str, 0, len)) != 0) len = cp - str; ptr = acl_allocator_membuf_alloc(filename, line, __var_allocator, len + 1); memcpy(ptr, str, len); ptr[len] = 0; return (ptr); }
static void vstring_extend(ACL_VBUF *bp, int incr) { unsigned used = (unsigned int) (bp->ptr - bp->data); long new_len; /* * Note: vp->vbuf.len is the current buffer size (both on entry and on * exit of this routine). We round up the increment size to the buffer * size to avoid silly little buffer increments. With really large * strings we might want to abandon the length doubling strategy, * and go to fixed increments. */ new_len = (long) (bp->len + (bp->len > incr ? bp->len : incr)); /* * bp->data = (unsigned char *) acl_myrealloc((char *) bp->data, * new_len); */ bp->data = acl_allocator_membuf_alloc(__FILE__, __LINE__, __var_allocator, new_len); bp->len = new_len; bp->ptr = bp->data + used; bp->cnt = bp->len - used; }
static void mempool_bench_test(const char *label, int mutex, int loop, acl_mem_type type, int size) { int i = 0; time_t begin = 0, end = 0; void *buf; #ifdef MUTEX_INIT acl_pthread_mutex_t lock; #elif defined(WIN32) acl_pthread_mutex_t lock; #define MUTEX_INIT #else acl_pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; #endif if (mutex) { #ifdef MUTEX_INIT acl_pthread_mutex_init(&lock, NULL); #endif time(&begin); while (i++ < loop) { acl_pthread_mutex_lock(&lock); acl_pthread_mutex_unlock(&lock); } time(&end); printf("lock and unkock, loop %d, time cost is %ld\r\n", loop, (long int) end - begin); i = 0; if (type > ACL_MEM_TYPE_NONE && type < ACL_MEM_TYPE_MAX) { time(&begin); while (i++ < loop) { acl_pthread_mutex_lock(&lock); buf = acl_allocator_mem_alloc(__var_allocator, type); acl_pthread_mutex_unlock(&lock); acl_pthread_mutex_lock(&lock); acl_allocator_mem_free(__var_allocator, type, buf); acl_pthread_mutex_unlock(&lock); } time(&end); } else if (type == MEM_TYPE_GROSS) { acl_pthread_mutex_lock(&lock); buf = acl_allocator_membuf_alloc(__FILE__, __LINE__, __var_allocator, size); acl_pthread_mutex_unlock(&lock); acl_pthread_mutex_lock(&lock); acl_allocator_membuf_free(__FILE__, __LINE__, __var_allocator, buf); acl_pthread_mutex_unlock(&lock); } else { time(&begin); while (i++ < loop) { acl_pthread_mutex_lock(&lock); buf = MALLOC(size); acl_pthread_mutex_unlock(&lock); acl_pthread_mutex_lock(&lock); FREE(buf); acl_pthread_mutex_unlock(&lock); } time(&end); } #ifdef MUTEX_INIT acl_pthread_mutex_destroy(&lock); #endif } else { if (type > ACL_MEM_TYPE_NONE && type < ACL_MEM_TYPE_MAX) { time(&begin); while (i++ < loop) { buf = acl_allocator_mem_alloc(__var_allocator, type); acl_allocator_mem_free(__var_allocator, type, buf); } time(&end); } else if (type == MEM_TYPE_GROSS) { buf = acl_allocator_membuf_alloc(__FILE__, __LINE__, __var_allocator, size); acl_allocator_membuf_free(__FILE__, __LINE__, __var_allocator, buf); } else { time(&begin); while (i++ < loop) { buf = MALLOC(size); FREE(buf); } time(&end); } } printf("%s: time cost is %ld seconds, count is %d\r\n", label, (long int) end - begin, loop); }
static void *mempool_malloc(const char *filename, int line, size_t size) { return (acl_allocator_membuf_alloc(filename, line, __var_allocator, size)); }