Max 5 API Reference
00001 /* 00002 jit.memory.c 00003 00004 Copyright 2001-2005 - Cycling '74 00005 Joshua Kit Clayton jkc@cycling74.com 00006 00007 */ 00008 00009 #include "jit.common.h" 00010 #include "jit.cpost.h" 00011 #include "ext_obex.h" 00012 00013 #ifdef WIN_VERSION 00014 //temporary windows memory crap. 00015 #define NewPtr malloc 00016 #define DisposePtr free 00017 #define jit_cpost post 00018 00019 #ifdef USE_HANDLE_MANIA 00020 #define MANIA_SIZE 10000 00021 long _handle_mania_init=0; 00022 char *_handle_mania[MANIA_SIZE]; 00023 #endif 00024 00025 #endif //WIN_VERSION 00026 00027 #define JIT_MEM_GETBYTES 0 00028 #define JIT_MEM_NEWPTR 1 00029 00030 long _jit_memreg=0; 00031 t_hashtab *_jit_memreg_hash=NULL; 00032 00033 /** 00034 * @defgroup memorymod Memory Module 00035 @ingroup jitter 00036 */ 00037 00038 00039 typedef struct _jit_memreg_entry 00040 { 00041 void *p; 00042 long size; 00043 long pool; 00044 } t_jit_memreg_entry; 00045 00046 void jit_memory_register(void *p, long size, long pool); 00047 void jit_memory_register(void *p, long size, long pool) 00048 { 00049 t_jit_memreg_entry *me=NULL; 00050 00051 if (!_jit_memreg_hash) 00052 _jit_memreg_hash = hashtab_new(997); 00053 00054 if (!p) 00055 jit_cpost("jit_memory_register ptr=%x size=%d pool=%d (NULL)",p,size,pool); 00056 else 00057 { 00058 hashtab_lookup(_jit_memreg_hash,(t_symbol *)p,(t_object **)&me); 00059 if (me) { 00060 if (_jit_memreg>1) { 00061 jit_cpost("jit_memory_register ptr=%x size=%d pool=%d (REDUNDANT MEMORY)",p,size,pool); 00062 jit_cpost("could be memory leak of harmless mismatch of jit_getbytes(), with max freebytes()"); 00063 } 00064 hashtab_chuckkey(_jit_memreg_hash,(t_symbol *)p); 00065 sysmem_freeptr(me); 00066 } 00067 me = (t_jit_memreg_entry *)sysmem_newptr(sizeof(t_jit_memreg_entry)); 00068 me->p = p; 00069 me->size = size; 00070 me->pool = pool; 00071 hashtab_store(_jit_memreg_hash,(t_symbol *)p,(t_object *)me); 00072 } 00073 } 00074 00075 void jit_memory_unregister(void *p, long size, long pool); 00076 void jit_memory_unregister(void *p, long size, long pool) 00077 { 00078 t_jit_memreg_entry *me=NULL; 00079 00080 if (!_jit_memreg_hash) 00081 _jit_memreg_hash = hashtab_new(997); 00082 00083 if (!p) 00084 jit_cpost("jit_memory_unregister ptr=%x size=%d pool=%d (NULL)",p,size,pool); 00085 else 00086 { 00087 hashtab_lookup(_jit_memreg_hash,(t_symbol *)p,(t_object **)&me); 00088 if (!me) 00089 jit_cpost("jit_memory_unregister ptr=%x size=%d pool=%d (NO REGISTERED ENTRY)",p,size,pool); 00090 else 00091 { 00092 hashtab_chuckkey(_jit_memreg_hash,(t_symbol *)p); 00093 if (me->pool!=pool) 00094 jit_cpost("jit_memory_unregister ptr=%x size=%d pool=%d (MISMATCHED POOL)",p,size,pool); 00095 if ((pool==JIT_MEM_GETBYTES)&&(me->size!=size)) 00096 jit_cpost("jit_memory_unregister ptr=%x size=%d pool=%d (MISMATCHED SIZE)",p,size,pool); 00097 00098 sysmem_freeptr(me); 00099 } 00100 } 00101 } 00102 00103 /** 00104 * Allocates a pointer to memory. 00105 * 00106 * Depending on the size requested, jit_getbytes will allocate from either the 00107 * faster memory pool or the system memory pool. 00108 * 00109 * @warning It is important to avoid mixing memory pools, and therefore to 00110 * match calls to jit_getbytes and jit_freebytes. 00111 * 00112 * @ingroup memorymod 00113 * 00114 * @param size size in bytes to allocate 00115 * 00116 * @return pointer to memory 00117 * 00118 */ 00119 void *jit_getbytes(long size) 00120 { 00121 void *rv; 00122 00123 rv = sysmem_newptr(size); 00124 00125 if (_jit_memreg) 00126 jit_memory_register(rv,size,JIT_MEM_GETBYTES); 00127 return rv; 00128 00129 } 00130 00131 /** 00132 * Frees a pointer to memory. 00133 * 00134 * Depending on the size of the pointer, jit_freebytes will free from either the 00135 * faster memory pool or the system memory pool. 00136 * 00137 * @warning It is important to avoid mixing memory pools, and therefore to 00138 * match calls to jit_getbytes and jit_freebytes. 00139 * 00140 * @ingroup memorymod 00141 * 00142 * @param ptr pointer to memory 00143 * @param size size in bytes allocated 00144 * 00145 */ 00146 void jit_freebytes(void *ptr,long size) 00147 { 00148 if (_jit_memreg) 00149 jit_memory_unregister(ptr,size,JIT_MEM_GETBYTES); 00150 00151 sysmem_freeptr(ptr); 00152 } 00153 00154 /** 00155 * Allocates a memory handle. 00156 * 00157 * Handles are relocatable sections of memory which should be locked 00158 * before dereferencing, and unlocked when not in use so that they may 00159 * be relocated as necessary. 00160 * 00161 * @warning It is important to avoid mixing memory pools, and therefore to 00162 * match calls to jit_handle_new and jit_handle_free. 00163 * 00164 * @ingroup memorymod 00165 * 00166 * @param size size in bytes to allocate 00167 * 00168 * @return memory handle 00169 * 00170 */ 00171 void **jit_handle_new(long size) 00172 { 00173 #ifdef USE_HANDLE_MANIA 00174 long i; 00175 00176 if (!_handle_mania_init) { 00177 _handle_mania_init=1; 00178 for (i=0;i<MANIA_SIZE;i++) _handle_mania[i]=0; 00179 } 00180 00181 for (i=0;i<MANIA_SIZE;i++) { 00182 if (_handle_mania[i]==0) 00183 goto gotit; 00184 } 00185 return (void **)NULL; 00186 00187 gotit: 00188 if (_handle_mania[i]=malloc(size)) 00189 return (void **)&(_handle_mania[i]); 00190 else 00191 return (void **)NULL; 00192 00193 #else 00194 return (void **)sysmem_newhandle(size); 00195 #endif 00196 } 00197 00198 /** 00199 * Frees a memory handle. 00200 * 00201 * @warning It is important to avoid mixing memory pools, and therefore to 00202 * match calls to jit_handle_new and jit_handle_free. 00203 * 00204 * @ingroup memorymod 00205 * 00206 * @param handle memory handle 00207 * 00208 */ 00209 void jit_handle_free(void **handle) 00210 { 00211 #ifdef USE_HANDLE_MANIA 00212 if (handle&&*handle) { 00213 free(*handle); 00214 *handle=NULL; 00215 } 00216 #else 00217 sysmem_freehandle((char **)handle); 00218 #endif 00219 } 00220 00221 /** 00222 * Retrieves a memory handle's size in bytes. 00223 * 00224 * @ingroup memorymod 00225 * 00226 * @param handle memory handle 00227 * 00228 * @return size in bytes 00229 * 00230 */ 00231 long jit_handle_size_get(void **handle) 00232 { 00233 #ifdef USE_HANDLE_MANIA 00234 if (handle&&*handle) { 00235 return _msize(*handle); 00236 } else { 00237 return 0; 00238 } 00239 #else 00240 return sysmem_handlesize((char **)handle); 00241 #endif 00242 } 00243 00244 /** 00245 * Sets a memory handle's size in bytes. 00246 * 00247 * @ingroup memorymod 00248 * 00249 * @param handle memory handle 00250 * @param size new size in bytes 00251 * 00252 * @return t_jit_err error code. 00253 * 00254 */ 00255 t_jit_err jit_handle_size_set(void **handle, long size) 00256 { 00257 #ifdef USE_HANDLE_MANIA 00258 if (handle&&*handle) { 00259 if (*handle=realloc(*handle,size)) 00260 return JIT_ERR_NONE; 00261 else 00262 return JIT_ERR_GENERIC; 00263 } else { 00264 return JIT_ERR_GENERIC; 00265 } 00266 #else 00267 return sysmem_resizehandle((char **)handle, size); 00268 #endif 00269 } 00270 00271 /** 00272 * Sets a memory handle's lock state. 00273 * 00274 * @ingroup memorymod 00275 * 00276 * @param handle memory handle 00277 * @param lock state (1=locked, 0=unlocked) 00278 * 00279 * @return lock state. 00280 * 00281 */ 00282 long jit_handle_lock(void **handle, long lock) 00283 { 00284 #ifdef USE_HANDLE_MANIA 00285 return !lock; 00286 #else 00287 return sysmem_lockhandle((char **)handle, lock); 00288 #endif 00289 } 00290 00291 /** 00292 * Copy bytes from source to destination pointer. 00293 * 00294 * @ingroup memorymod 00295 * 00296 * @param dest destination pointer 00297 * @param src source pointer 00298 * @param bytes byte count to copy 00299 * 00300 */ 00301 void jit_copy_bytes(void *dest, const void *src, long bytes) 00302 { 00303 BlockMoveData(src, dest, bytes); 00304 } 00305 00306 /** 00307 * Reports free memory. 00308 * 00309 * @warning Obsolete. OS 9 only. 00310 * 00311 * @ingroup memorymod 00312 * 00313 * @return free bytes 00314 * 00315 */ 00316 long jit_freemem(void) 00317 { 00318 return FreeMem(); 00319 } 00320 00321 /** 00322 * Allocates a pointer to memory. 00323 * 00324 * Always allocates from the the system memory pool. 00325 * 00326 * @warning It is important to avoid mixing memory pools, and therefore to 00327 * match calls to jit_newptr and jit_disposeptr. 00328 * 00329 * @ingroup memorymod 00330 * 00331 * @param size size in bytes to allocate 00332 * 00333 * @return pointer to memory 00334 * 00335 */ 00336 char *jit_newptr(long size) 00337 { 00338 char *rv = sysmem_newptr(size); 00339 00340 if (_jit_memreg) 00341 jit_memory_register(rv,size,JIT_MEM_NEWPTR); 00342 00343 return rv; 00344 } 00345 00346 /** 00347 * Frees a pointer to memory. 00348 * 00349 * @warning It is important to avoid mixing memory pools, and therefore to 00350 * match calls to jit_newptr and jit_disposeptr. 00351 * 00352 * @ingroup memorymod 00353 * 00354 * @param ptr pointer to memory 00355 * 00356 */ 00357 void jit_disposeptr(char *ptr) 00358 { 00359 if (_jit_memreg) 00360 jit_memory_unregister((void *)ptr,0,JIT_MEM_NEWPTR); 00361 00362 sysmem_freeptr(ptr); 00363 } 00364 00365 00366 00367
Copyright © 2008, Cycling '74