| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <pthread.h>
- #include <unistd.h>
- #include "valloc.h"
- #define HEADER_SIZE sizeof(int)
- static pthread_mutex_t mutex;
- static int count = 0;
- static int use = 0;
- void *v_malloc(size_t size)
- {
- if (size == 0)
- {
- return NULL;
- }
- void *p = malloc(size + HEADER_SIZE);
- if (!p)
- {
- return NULL;
- }
- *(int *)p = (int)size;
- pthread_mutex_lock(&mutex);
- count++;
- use += (int)size;
- pthread_mutex_unlock(&mutex);
- return (char *)p + HEADER_SIZE;
- }
- void *v_calloc(size_t num, size_t size)
- {
- size_t total = num * size;
- void *p = v_malloc(total);
- if (p)
- {
- memset(p, 0, total);
- }
- return p;
- }
- void v_free(void *block)
- {
- if (!block)
- {
- return;
- }
- void *p = (char *)block - HEADER_SIZE;
- int size = *(int *)p;
- pthread_mutex_lock(&mutex);
- count--;
- use -= size;
- pthread_mutex_unlock(&mutex);
- free(p);
- }
- void *v_realloc(void *block, size_t size)
- {
- if (size == 0)
- {
- v_free(block);
- return NULL;
- }
- int old_size = 0;
- void *raw = NULL;
- if (block)
- {
- raw = (char *)block - HEADER_SIZE;
- old_size = *(int *)raw;
- }
- void *p = realloc(raw, size + HEADER_SIZE);
- if (!p)
- {
- return NULL;
- }
- *(int *)p = (int)size;
- pthread_mutex_lock(&mutex);
- if (!block)
- {
- count++;
- }
- use += (int)(size - old_size);
- pthread_mutex_unlock(&mutex);
- return (char *)p + HEADER_SIZE;
- }
- int v_mcheck(int *dstCount, int *dstUse)
- {
- pthread_mutex_lock(&mutex);
- if (dstCount)
- {
- *dstCount = count;
- }
- if (dstUse)
- {
- *dstUse = use;
- }
- pthread_mutex_unlock(&mutex);
- return 0;
- }
- void displayMem(void)
- {
- int32_t area2 = 0, use2 = 0;
- v_mcheck(&area2, &use2);
- printf("|||----------->>> area = %d, size = %d\r\n", area2, use2);
- }
- void vallocInit(void)
- {
- pthread_mutex_init(&mutex, NULL);
- }
|