Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include "memoryPool.h"
- /* ストア */
- typedef struct _store* store;
- struct _store {
- store next; /* 次ストア */
- void* data; /* データ */
- };
- /* プールハンドル */
- struct _poolHandle {
- long providableByte; /* 提供可能なサイズ */
- void* candi; /* 提供候補 */
- store first;
- store last;
- };
- poolHandle memoryPool_Create() {
- void* p = NULL;
- p = malloc( sizeof( struct _poolHandle ) );
- if( p ) {
- memset( p, 0x00, sizeof( struct _poolHandle ) );
- }
- return p;
- }
- void* memoryPool_Alloc( poolHandle handle, long byte ) {
- if( !handle || byte == 0 ) {
- return NULL;
- }
- if( ALLOC_BYTE < byte ) {
- return NULL;
- }
- if( byte <= handle->providableByte ) {
- /* 提供可能 */
- handle->providableByte -= byte;
- void* ret = handle->candi;
- (long)handle->candi += byte + 1;
- return ret;
- }
- else {
- /* 提供不可 */
- store s = ( store )malloc( sizeof( struct _store ) );
- if( !s ) {
- return NULL;
- }
- memset( s, 0x00, sizeof( struct _store ) );
- s->data = malloc( ALLOC_BYTE );
- if( !( s->data ) ) {
- return NULL;
- }
- memset( s->data, 0x00, ALLOC_BYTE );
- if( !( handle->first ) ) {
- /* 既存ストア無し */
- handle->first = handle->last = s;
- }
- else {
- /* 既存ストアあり */
- handle->last->next = s;
- handle->last = s;
- }
- handle->providableByte = ALLOC_BYTE - byte;
- //void* ret = handle->candi;
- handle->candi = (long)s->data + byte + 1;
- return ( long )s->data;
- }
- }
- void memoryPool_Release( poolHandle handle ) {
- if( !handle ) {
- return;
- }
- store s = handle->first;
- if( !s ) return;
- do {
- store t = s;
- s = s->next;
- free( t->data );
- free( t );
- } while( s );
- free( handle );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement