graphicscomposition/openwfcompositionengine/common/src/owfpool.c
changeset 0 5d03bc08d59c
child 152 9f1c3fea0f87
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 /* Copyright (c) 2009 The Khronos Group Inc.
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and/or associated documentation files (the
       
     5  * "Materials"), to deal in the Materials without restriction, including
       
     6  * without limitation the rights to use, copy, modify, merge, publish,
       
     7  * distribute, sublicense, and/or sell copies of the Materials, and to
       
     8  * permit persons to whom the Materials are furnished to do so, subject to
       
     9  * the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
       
    18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
       
    19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  */
       
    22 
       
    23  /*
       
    24  * owfpool.c
       
    25  */
       
    26 
       
    27 
       
    28 #ifdef __cplusplus
       
    29 extern "C"
       
    30 {
       
    31 #endif
       
    32 
       
    33 
       
    34 #include <string.h>
       
    35 #include <stdio.h>
       
    36 #include <stdlib.h>
       
    37 
       
    38 #include "owfdebug.h"
       
    39 #include "owftypes.h"
       
    40 #include "owfpool.h"
       
    41 #include "owfmemory.h"
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 OWF_API_CALL OWF_POOL*
       
    47 OWF_Pool_Create(size_t objectSize, size_t objectCount)
       
    48 {
       
    49     OWF_POOL*               pool;
       
    50     char*                   chunk;
       
    51     OWFuint32*               entries;
       
    52 
       
    53     /* we be needing some memories, aye */
       
    54     pool    = (OWF_POOL*)xalloc(sizeof(OWF_POOL), 1);
       
    55     entries = (OWFuint32*)xalloc(sizeof(OWFuint32), objectCount);
       
    56     chunk   = (char*)xalloc((objectSize + sizeof(OWFuint32*)), objectCount);
       
    57 
       
    58     if (pool && entries && chunk)
       
    59     {
       
    60         size_t              i;
       
    61 
       
    62         /* setup pool */
       
    63         pool->entries   = entries;
       
    64         pool->chunk     = chunk;
       
    65         pool->capacity  =
       
    66         pool->free      = objectCount;
       
    67         pool->firstFree = 0;
       
    68         pool->entrySize = objectSize;
       
    69 
       
    70         /* initially all objects are marked as free */
       
    71         for (i = 0; i < objectCount-1; i++) {
       
    72             entries[i] = i+1;
       
    73         }
       
    74         /* terminate chain */
       
    75         entries[objectCount-1] = EOC;
       
    76     }
       
    77     else
       
    78     {
       
    79         /* failed miserably. bail out. */
       
    80         xfree(pool);
       
    81         pool = NULL;
       
    82         xfree(entries);
       
    83         xfree(chunk);
       
    84     }
       
    85 
       
    86     return pool;
       
    87 }
       
    88 
       
    89 OWF_API_CALL void*
       
    90 OWF_Pool_GetObject(OWF_POOL* pool)
       
    91 {
       
    92     void*                   object = NULL;
       
    93 
       
    94     if (pool)
       
    95     {
       
    96         /* we have objects left in the pool? */
       
    97         if (pool->firstFree != EOC)
       
    98         {
       
    99             OWFuint32*       temp;
       
   100             OWFuint32        chunk;
       
   101 
       
   102             chunk = (OWFuint32) pool->chunk;
       
   103 
       
   104             /* calculate offset inside the chunk */
       
   105             temp = (OWFuint32*) (chunk + (pool->entrySize +
       
   106                     sizeof(OWFuint32*)) * pool->firstFree);
       
   107             /* remember way back to home */
       
   108             temp[0] = (OWFuint32) pool;
       
   109             object = (void*) &temp[1];
       
   110             pool->firstFree = pool->entries[pool->firstFree];
       
   111             --pool->free;
       
   112 
       
   113             OWF_ASSERT((OWFuint32) object > (OWFuint32) pool->chunk);
       
   114             OWF_ASSERT((int)object != 0x5eaf00d);
       
   115         }
       
   116     }
       
   117     return object;
       
   118 }
       
   119 
       
   120 OWF_API_CALL void
       
   121 OWF_Pool_PutObject(void* object)
       
   122 {
       
   123     OWFuint32* temp = (OWFuint32*) object;
       
   124 
       
   125     if (temp && temp[-1])
       
   126     {
       
   127         OWF_POOL*           pool;
       
   128         OWFuint32            addr;
       
   129         OWFuint32            chunk;
       
   130         OWFuint32            entrySize;
       
   131 
       
   132         addr    = (OWFuint32) object - 4;
       
   133         pool    = (OWF_POOL*) temp[-1];
       
   134         chunk   = (OWFuint32) pool->chunk;
       
   135         entrySize = pool->entrySize + sizeof(OWFuint32*);
       
   136 
       
   137         if (addr >= chunk &&
       
   138             addr < (chunk + pool->capacity * entrySize))
       
   139         {
       
   140             OWFuint32 index = (addr - chunk) / entrySize;
       
   141 
       
   142             pool->entries[index] = pool->firstFree;
       
   143             pool->firstFree = index;
       
   144             memset(object, 0x0, pool->entrySize);
       
   145             ++pool->free;
       
   146         }
       
   147         else
       
   148         {
       
   149             /* invalid object */
       
   150             OWF_ASSERT(0);
       
   151         }
       
   152     }
       
   153 }
       
   154 
       
   155 OWF_API_CALL void
       
   156 OWF_Pool_Destroy(OWF_POOL* pool)
       
   157 {
       
   158     if (pool)
       
   159     {
       
   160         xfree(pool->entries);
       
   161         xfree(pool->chunk);
       
   162         memset(pool, 0, sizeof(OWF_POOL));
       
   163         xfree(pool);
       
   164     }
       
   165 }
       
   166 
       
   167 
       
   168 #ifdef __cplusplus
       
   169 }
       
   170 #endif