home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / asm / chnkpool.c < prev    next >
C/C++ Source or Header  |  2008-04-01  |  3KB  |  112 lines

  1. /*
  2.  * Copyright (c) 2003 - 2005 Magnus Lind.
  3.  *
  4.  * This software is provided 'as-is', without any express or implied warranty.
  5.  * In no event will the authors be held liable for any damages arising from
  6.  * the use of this software.
  7.  *
  8.  * Permission is granted to anyone to use this software, alter it and re-
  9.  * distribute it freely for any non-commercial, non-profit purpose subject to
  10.  * the following restrictions:
  11.  *
  12.  *   1. The origin of this software must not be misrepresented; you must not
  13.  *   claim that you wrote the original software. If you use this software in a
  14.  *   product, an acknowledgment in the product documentation would be
  15.  *   appreciated but is not required.
  16.  *
  17.  *   2. Altered source versions must be plainly marked as such, and must not
  18.  *   be misrepresented as being the original software.
  19.  *
  20.  *   3. This notice may not be removed or altered from any distribution.
  21.  *
  22.  *   4. The names of this software and/or it's copyright holders may not be
  23.  *   used to endorse or promote products derived from this software without
  24.  *   specific prior written permission.
  25.  *
  26.  */
  27.  
  28. #include "chnkpool.h"
  29. #include "log.h"
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33.  
  34. void
  35. chunkpool_init(struct chunkpool *ctx, int size)
  36. {
  37.     ctx->chunk_size = size;
  38.     ctx->chunk = -1;
  39.     ctx->chunk_max = (0x1fffff / size) * size;
  40.     ctx->chunk_pos = ctx->chunk_max;
  41. }
  42.  
  43. void
  44. chunkpool_free2(struct chunkpool *ctx, cb_free *f)
  45. {
  46.     while(ctx->chunk >= 0)
  47.     {
  48.         if(f != NULL)
  49.         {
  50.             do
  51.             {
  52.                 ctx->chunk_pos -= ctx->chunk_size;
  53.                 f((char*)ctx->chunks[ctx->chunk] + ctx->chunk_pos);
  54.             }
  55.             while(ctx->chunk_pos > 0);
  56.             ctx->chunk_pos = ctx->chunk_max;
  57.         }
  58.     free(ctx->chunks[ctx->chunk]);
  59.     ctx->chunk -= 1;
  60.     }
  61.     ctx->chunk_size = -1;
  62.     ctx->chunk_max = -1;
  63.     ctx->chunk_pos = -1;
  64. }
  65.  
  66. void
  67. chunkpool_free(struct chunkpool *ctx)
  68. {
  69.     chunkpool_free2(ctx, NULL);
  70. }
  71.  
  72. void *
  73. chunkpool_malloc(struct chunkpool *ctx)
  74. {
  75.     void *p;
  76.     if(ctx->chunk_pos == ctx->chunk_max)
  77.     {
  78.     void *m;
  79.     if(ctx->chunk == CHUNKPOOL_CHUNKS_MAX - 1)
  80.     {
  81.         LOG(LOG_ERROR, ("out of chunks in file %s, line %d\n",
  82.                 __FILE__, __LINE__));
  83.         LOG(LOG_BRIEF, ("chunk_size %d\n", ctx->chunk_size));
  84.         LOG(LOG_BRIEF, ("chunk_max %d\n", ctx->chunk_max));
  85.         LOG(LOG_BRIEF, ("chunk %d\n", ctx->chunk));
  86.         exit(-1);
  87.     }
  88.     m = malloc(ctx->chunk_max);
  89.         LOG(LOG_DEBUG, ("allocating new chunk %p\n", m));
  90.     if (m == NULL)
  91.     {
  92.         LOG(LOG_ERROR, ("out of memory error in file %s, line %d\n",
  93.                 __FILE__, __LINE__));
  94.         exit(-1);
  95.     }
  96.     ctx->chunk += 1;
  97.     ctx->chunks[ctx->chunk] = m;
  98.     ctx->chunk_pos = 0;
  99.     }
  100.     p = (char*)ctx->chunks[ctx->chunk] + ctx->chunk_pos;
  101.     ctx->chunk_pos += ctx->chunk_size;
  102.     return p;
  103. }
  104.  
  105. void *
  106. chunkpool_calloc(struct chunkpool *ctx)
  107. {
  108.     void *p = chunkpool_malloc(ctx);
  109.     memset(p, 0, ctx->chunk_size);
  110.     return p;
  111. }
  112.