Last active
December 10, 2016 10:55
-
-
Save otaks/0175dd923d20eff44e0dbf355d9f601c to your computer and use it in GitHub Desktop.
memory pool
This file contains 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 <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 ); | |
} |
This file contains 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
#pragma once | |
#define ALLOC_BYTE 1024 /* 一括確保サイズ */ | |
typedef struct _poolHandle* poolHandle; /* プールハンドル */ | |
/** | |
*生成。 | |
* | |
*@return NULL以外:プールハンドル、NULL:失敗 | |
*/ | |
poolHandle memoryPool_Create(); | |
/** | |
*メモリ確保。 | |
*ALLOC_BYTEより大きいbyteは指定不可。 | |
* | |
*@param handle プールハンドル | |
*@param byte 確保するメモリ量(バイト) | |
*@return NULL以外:メモリアドレス、NULL:失敗 | |
*/ | |
void* memoryPool_Alloc( poolHandle handle, long size ); | |
/** | |
*解放。 | |
* | |
*@param handle | |
*/ | |
void memoryPool_Release( poolHandle handle ); |
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "memoryPool.h" | |
void test1() { | |
poolHandle h = NULL; | |
h = memoryPool_Create(); | |
if( !h ) { | |
printf( "memoryPool_Create() err\n" ); | |
exit( 1 ); | |
} | |
int* i; | |
if( !( i = ( int* )memoryPool_Alloc( h, sizeof( int ) ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
*i = 3; | |
printf( "i=%d\n", *i ); | |
memoryPool_Release( h ); | |
} | |
void test2() { | |
poolHandle h = NULL; | |
h = memoryPool_Create(); | |
if( !h ) { | |
printf( "memoryPool_Create() err\n" ); | |
exit( 1 ); | |
} | |
int* i; | |
if( !( i = ( int* )memoryPool_Alloc( h, 1024 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
if( !( i = ( int* )memoryPool_Alloc( h, 1024 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
if( !( i = ( int* )memoryPool_Alloc( h, 1024 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
memoryPool_Release( h ); | |
} | |
void test4() { | |
poolHandle h = NULL; | |
h = memoryPool_Create(); | |
if( !h ) { | |
printf( "memoryPool_Create() err\n" ); | |
exit( 1 ); | |
} | |
int* i; | |
if( !( i = ( int* )memoryPool_Alloc( h, 1024 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
struct st { | |
int a; | |
long b; | |
char* c; | |
}; | |
struct st* p; | |
if( !( p = ( struct st* )memoryPool_Alloc( h, sizeof(struct st) ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
if( !( p->c = ( char* )memoryPool_Alloc( h, 10 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
if( !( i = ( int* )memoryPool_Alloc( h, 1024 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
p->a = 4; | |
p->b = 13; | |
strcpy( p->c, "hello...." ); | |
printf( "a=%d, b=%d, c=%s\n", p->a, p->b, p->c ); | |
memoryPool_Release( h ); | |
} | |
void test3() { | |
poolHandle h = NULL; | |
h = memoryPool_Create(); | |
if( !h ) { | |
printf( "memoryPool_Create() err\n" ); | |
exit( 1 ); | |
} | |
struct st { | |
int a; | |
long b; | |
char* c; | |
}; | |
struct st* p; | |
if( !( p = ( struct st* )memoryPool_Alloc( h, sizeof( struct st ) ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
if( !( p->c = ( char* )memoryPool_Alloc( h, 10 ) ) ) { | |
printf( "memoryPool_Alloc() err\n" ); | |
exit( 1 ); | |
} | |
p->a = 4; | |
p->b = 13; | |
strcpy( p->c, "hello...." ); | |
printf( "a=%d, b=%d, c=%s\n", p->a, p->b, p->c ); | |
memoryPool_Release( h ); | |
} | |
int main() { | |
test1(); | |
test2(); | |
test3(); | |
test4(); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment