Created
May 17, 2014 03:28
-
-
Save YoukouTenhouin/e6beb32d605c183520d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mempool.h" | |
#include <stdlib.h> | |
void | |
mempool_new(struct hmd_mempool *pool) | |
{ | |
struct hmd_mempool_chunk *chunk; | |
chunk = malloc(sizeof(struct hmd_mempool_chunk)); | |
chunk->prev = NULL; | |
chunk->next = NULL; | |
chunk->available = CHUNK_SIZE; | |
pool->chunks = chunk; | |
pool->larges = NULL; | |
} | |
void | |
mempool_destory(struct hmd_mempool *pool) | |
{ | |
struct hmd_mempool_chunk *chunk_iter = pool->chunks; | |
while (chunk_iter != NULL) { | |
struct hmd_mempool_chunk *tmp = chunk_iter; | |
chunk_iter = chunk_iter->next; | |
free(tmp); | |
} | |
struct hmd_mempool_large *large_iter = pool->larges; | |
while (large_iter != NULL) { | |
struct hmd_mempool_large *tmp = large_iter; | |
large_iter = large_iter->next; | |
free(tmp->data); | |
free(tmp); | |
} | |
} | |
static struct hmd_mempool_chunk* | |
insert_chunk_to_list( | |
struct hmd_mempool_chunk *list, | |
struct hmd_mempool_chunk *chunk) | |
{ | |
struct hmd_mempool_chunk *iter = list; | |
while(iter->next != NULL && (iter->available > chunk->available)) | |
iter = iter->next; | |
chunk->next = iter; | |
chunk->prev = iter->prev; | |
chunk->prev->next = chunk; | |
iter->prev = chunk; | |
return chunk->prev == NULL?chunk:list; | |
} | |
void* | |
mempool_alloc(struct hmd_mempool *pool,int size) | |
{ | |
if (size > CHUNK_SIZE/4) { | |
void *data = malloc(size); | |
if (data == NULL) return NULL; | |
struct hmd_mempool_large *new_large; | |
new_large = malloc(struct hmd_mempool_large); | |
new_large->data = data; | |
new_large->next = pool->larges; | |
new_large->prev = NULL; | |
pool->larges->prev = new_large; | |
pool->larges = new_large; | |
return data; | |
} else { | |
struct hmd_mempool_chunk *chunk,*list; | |
if(pool->chunks->available > size){ | |
chunk = pool->chunks; | |
list = chunk->next; | |
list->prev = NULL; | |
} else { | |
chunk = malloc(sizeof(struct hmd_mempool_chunk)); | |
if (chunk == NULL) return NULL; | |
list = pool->chunks; | |
} | |
void *ret = (void*)chunk->curr; | |
chunk->curr += size; | |
list = insert_chunk_to_list(list,chunk); | |
pool->chunks = list; | |
return ret; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HMD_MEMPOOL_H__ | |
#define __HMD_MEMPOOL_H__ | |
#define CHUNK_SIZE = 4096 | |
struct hmd_mempool_chunk | |
{ | |
struct hmd_mempool_chunk *prev,*next; | |
unsigned char data[CHUNK_SIZE]; | |
unsigned char *curr; | |
int available; | |
}; | |
struct hmd_mempool_large | |
{ | |
struct hmd_mempool_large *prev,*next; | |
void *data; | |
}; | |
struct hmd_mempool | |
{ | |
struct hmd_mempool_chunk *chunks; | |
struct hmd_mempool_large *larges; | |
}; | |
void mempool_new(struct hmd_mempool *pool); | |
void mempool_destroy(struct hmd_mempool *pool); | |
void* mempool_alloc(struct hmd_mempool *pool,int size); | |
#endif // __HMD_MEMPOOL_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment