Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "memoryPool.h"
  4.  
  5. /* ストア */
  6. typedef struct _store* store;
  7. struct _store {
  8. store next; /* 次ストア */
  9. void* data; /* データ */
  10. };
  11.  
  12. /* プールハンドル */
  13. struct _poolHandle {
  14. long providableByte; /* 提供可能なサイズ */
  15. void* candi; /* 提供候補 */
  16. store first;
  17. store last;
  18. };
  19.  
  20.  
  21. poolHandle memoryPool_Create() {
  22. void* p = NULL;
  23. p = malloc( sizeof( struct _poolHandle ) );
  24. if( p ) {
  25. memset( p, 0x00, sizeof( struct _poolHandle ) );
  26. }
  27. return p;
  28. }
  29.  
  30.  
  31. void* memoryPool_Alloc( poolHandle handle, long byte ) {
  32. if( !handle || byte == 0 ) {
  33. return NULL;
  34. }
  35. if( ALLOC_BYTE < byte ) {
  36. return NULL;
  37. }
  38.  
  39. if( byte <= handle->providableByte ) {
  40. /* 提供可能 */
  41. handle->providableByte -= byte;
  42. void* ret = handle->candi;
  43. (long)handle->candi += byte + 1;
  44. return ret;
  45. }
  46. else {
  47. /* 提供不可 */
  48. store s = ( store )malloc( sizeof( struct _store ) );
  49. if( !s ) {
  50. return NULL;
  51. }
  52. memset( s, 0x00, sizeof( struct _store ) );
  53. s->data = malloc( ALLOC_BYTE );
  54. if( !( s->data ) ) {
  55. return NULL;
  56. }
  57. memset( s->data, 0x00, ALLOC_BYTE );
  58.  
  59. if( !( handle->first ) ) {
  60. /* 既存ストア無し */
  61. handle->first = handle->last = s;
  62. }
  63. else {
  64. /* 既存ストアあり */
  65. handle->last->next = s;
  66. handle->last = s;
  67. }
  68.  
  69. handle->providableByte = ALLOC_BYTE - byte;
  70. //void* ret = handle->candi;
  71. handle->candi = (long)s->data + byte + 1;
  72. return ( long )s->data;
  73. }
  74. }
  75.  
  76.  
  77. void memoryPool_Release( poolHandle handle ) {
  78. if( !handle ) {
  79. return;
  80. }
  81.  
  82. store s = handle->first;
  83. if( !s ) return;
  84.  
  85. do {
  86. store t = s;
  87. s = s->next;
  88.  
  89. free( t->data );
  90. free( t );
  91. } while( s );
  92.  
  93. free( handle );
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement