Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenslang_mem.c
Go to the documentation of this file.
00001 /* 00002 * Mesa 3-D graphics library 00003 * Version: 6.5.3 00004 * 00005 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved. 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a 00008 * copy of this software and associated documentation files (the "Software"), 00009 * to deal in the Software without restriction, including without limitation 00010 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 * and/or sell copies of the Software, and to permit persons to whom the 00012 * Software is furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included 00015 * in all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00018 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00021 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00022 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 00035 #include "main/context.h" 00036 #include "main/macros.h" 00037 #include "slang_mem.h" 00038 00039 00040 #define GRANULARITY 8 00041 #define ROUND_UP(B) ( ((B) + (GRANULARITY - 1)) & ~(GRANULARITY - 1) ) 00042 00043 00045 #define USE_MALLOC_FREE 0 00046 00047 00048 struct slang_mempool_ 00049 { 00050 GLuint Size, Used, Count, Largest; 00051 char *Data; 00052 struct slang_mempool_ *Next; 00053 }; 00054 00055 00056 slang_mempool * 00057 _slang_new_mempool(GLuint initialSize) 00058 { 00059 slang_mempool *pool = (slang_mempool *) _mesa_calloc(sizeof(slang_mempool)); 00060 if (pool) { 00061 pool->Data = (char *) _mesa_calloc(initialSize); 00062 /*printf("ALLOC MEMPOOL %d at %p\n", initialSize, pool->Data);*/ 00063 if (!pool->Data) { 00064 _mesa_free(pool); 00065 return NULL; 00066 } 00067 pool->Size = initialSize; 00068 pool->Used = 0; 00069 } 00070 return pool; 00071 } 00072 00073 00074 void 00075 _slang_delete_mempool(slang_mempool *pool) 00076 { 00077 GLuint total = 0; 00078 while (pool) { 00079 slang_mempool *next = pool->Next; 00080 /* 00081 printf("DELETE MEMPOOL %u / %u count=%u largest=%u\n", 00082 pool->Used, pool->Size, pool->Count, pool->Largest); 00083 */ 00084 total += pool->Used; 00085 _mesa_free(pool->Data); 00086 _mesa_free(pool); 00087 pool = next; 00088 } 00089 /*printf("TOTAL ALLOCATED: %u\n", total);*/ 00090 } 00091 00092 00093 #ifdef DEBUG 00094 static void 00095 check_zero(const char *addr, GLuint n) 00096 { 00097 GLuint i; 00098 for (i = 0; i < n; i++) { 00099 assert(addr[i]==0); 00100 } 00101 } 00102 #endif 00103 00104 00105 #ifdef DEBUG 00106 static GLboolean 00107 is_valid_address(const slang_mempool *pool, void *addr) 00108 { 00109 while (pool) { 00110 if ((char *) addr >= pool->Data && 00111 (char *) addr < pool->Data + pool->Used) 00112 return GL_TRUE; 00113 00114 pool = pool->Next; 00115 } 00116 return GL_FALSE; 00117 } 00118 #endif 00119 00120 00124 void * 00125 _slang_alloc(GLuint bytes) 00126 { 00127 #if USE_MALLOC_FREE 00128 return _mesa_calloc(bytes); 00129 #else 00130 slang_mempool *pool; 00131 GET_CURRENT_CONTEXT(ctx); 00132 pool = (slang_mempool *) ctx->Shader.MemPool; 00133 00134 if (bytes == 0) 00135 bytes = 1; 00136 00137 while (pool) { 00138 if (pool->Used + bytes <= pool->Size) { 00139 /* found room */ 00140 void *addr = (void *) (pool->Data + pool->Used); 00141 #ifdef DEBUG 00142 check_zero((char*) addr, bytes); 00143 #endif 00144 pool->Used += ROUND_UP(bytes); 00145 pool->Largest = MAX2(pool->Largest, bytes); 00146 pool->Count++; 00147 /*printf("alloc %u Used %u\n", bytes, pool->Used);*/ 00148 return addr; 00149 } 00150 else if (pool->Next) { 00151 /* try next block */ 00152 pool = pool->Next; 00153 } 00154 else { 00155 /* alloc new pool */ 00156 const GLuint sz = MAX2(bytes, pool->Size); 00157 pool->Next = _slang_new_mempool(sz); 00158 if (!pool->Next) { 00159 /* we're _really_ out of memory */ 00160 return NULL; 00161 } 00162 else { 00163 pool = pool->Next; 00164 pool->Largest = bytes; 00165 pool->Count++; 00166 pool->Used = ROUND_UP(bytes); 00167 #ifdef DEBUG 00168 check_zero((char*) pool->Data, bytes); 00169 #endif 00170 return (void *) pool->Data; 00171 } 00172 } 00173 } 00174 return NULL; 00175 #endif 00176 } 00177 00178 00179 void * 00180 _slang_realloc(void *oldBuffer, GLuint oldSize, GLuint newSize) 00181 { 00182 #if USE_MALLOC_FREE 00183 return _mesa_realloc(oldBuffer, oldSize, newSize); 00184 #else 00185 GET_CURRENT_CONTEXT(ctx); 00186 slang_mempool *pool = (slang_mempool *) ctx->Shader.MemPool; 00187 (void) pool; 00188 00189 if (newSize < oldSize) { 00190 return oldBuffer; 00191 } 00192 else { 00193 const GLuint copySize = (oldSize < newSize) ? oldSize : newSize; 00194 void *newBuffer = _slang_alloc(newSize); 00195 00196 if (oldBuffer) 00197 ASSERT(is_valid_address(pool, oldBuffer)); 00198 00199 if (newBuffer && oldBuffer && copySize > 0) 00200 _mesa_memcpy(newBuffer, oldBuffer, copySize); 00201 00202 return newBuffer; 00203 } 00204 #endif 00205 } 00206 00207 00211 char * 00212 _slang_strdup(const char *s) 00213 { 00214 if (s) { 00215 size_t l = _mesa_strlen(s); 00216 char *s2 = (char *) _slang_alloc(l + 1); 00217 if (s2) 00218 _mesa_strcpy(s2, s); 00219 return s2; 00220 } 00221 else { 00222 return NULL; 00223 } 00224 } 00225 00226 00230 void 00231 _slang_free(void *addr) 00232 { 00233 #if USE_MALLOC_FREE 00234 _mesa_free(addr); 00235 #else 00236 if (addr) { 00237 GET_CURRENT_CONTEXT(ctx); 00238 slang_mempool *pool = (slang_mempool *) ctx->Shader.MemPool; 00239 (void) pool; 00240 ASSERT(is_valid_address(pool, addr)); 00241 } 00242 #endif 00243 } Generated on Fri May 25 2012 04:18:49 for ReactOS by
1.7.6.1
|