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