Update contrib.
1 /* Copyright (c) 2009 The Khronos Group Inc.
2 * Portions copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Materials.
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.
42 #include "owfmemory.h"
47 OWF_API_CALL OWF_POOL*
48 OWF_Pool_Create(size_t objectSize, size_t objectCount)
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);
59 if (pool && entries && chunk)
64 pool->entries = entries;
67 pool->free = objectCount;
69 pool->entrySize = objectSize;
71 /* initially all objects are marked as free */
72 for (i = 0; i < objectCount-1; i++) {
76 entries[objectCount-1] = EOC;
80 /* failed miserably. bail out. */
91 OWF_Pool_GetObject(OWF_POOL* pool)
97 /* we have objects left in the pool? */
98 if (pool->firstFree != EOC)
103 chunk = (OWFuint32) pool->chunk;
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];
114 OWF_ASSERT((OWFuint32) object > (OWFuint32) pool->chunk);
115 OWF_ASSERT((int)object != 0x5eaf00d);
122 OWF_Pool_PutObject(void* object)
124 OWFuint32* temp = (OWFuint32*) object;
126 if (temp && temp[-1])
133 addr = (OWFuint32) object - 4;
134 pool = (OWF_POOL*) temp[-1];
135 chunk = (OWFuint32) pool->chunk;
136 entrySize = pool->entrySize + sizeof(OWFuint32*);
139 addr < (chunk + pool->capacity * entrySize))
141 OWFuint32 index = (addr - chunk) / entrySize;
143 pool->entries[index] = pool->firstFree;
144 pool->firstFree = index;
145 memset(object, 0x0, pool->entrySize);
157 OWF_Pool_Destroy(OWF_POOL* pool)
161 xfree(pool->entries);
163 memset(pool, 0, sizeof(OWF_POOL));