00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
#ifndef PI_BasicTypes_h
00014 
#include "PI_Basic.h"
00015 
#endif
00016 
00017 
#ifndef PI_Memory_h
00018 
#include "PI_Mem.h"
00019 
#endif
00020 
00021 
#ifndef PI_Machine_h
00022 
#include "PI_Mach.h"
00023 
#endif
00024 
00025 
#include <string.h>     
00026 
#ifdef IntelMode
00027 
#include "PI_Swap.h"
00028 
#endif
00029 
00030 
#if __IS_MAC
00031 
void Debugger();
00032 
#endif
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 Ptr SmartNewPtr(Size byteCount,
00048                                 OSErr* resultCode)
00049 {
00050         
Ptr aPtr;
00051         aPtr = (
Ptr)
LH_malloc(byteCount);
00052         
if (aPtr == 0)
00053                 *resultCode = 
notEnoughMemoryErr;
00054         
else
00055                 *resultCode = 0;
00056         
return aPtr;
00057 }
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 Ptr SmartNewPtrClear(Size byteCount,
00073                                          OSErr* resultCode)
00074 {
00075         
Ptr ptr = 
NULL;
00076 
00077         ptr = 
SmartNewPtr(byteCount, resultCode);
00078 
00079         
if (ptr != 
NULL)
00080         {
00081                 memset( ptr, 0, byteCount );
00082         }
00083         
return ptr;
00084 
00085 }
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 Ptr DisposeIfPtr(Ptr thePtr)
00100 {
00101         
if (thePtr)
00102         {
00103                 
LH_free(thePtr);
00104         }
00105         
return NULL;
00106 }
00107 
00108 
#ifdef __MWERKS__
00109 
extern pascal 
Ptr NewPtr(Size byteCount);
00110 
extern pascal 
void DisposePtr(Ptr p);
00111 
#endif  
00112 
00113 
#ifdef LH_MEMORY_DEBUG
00114 
typedef struct
00115 
{
00116         
void* p;
00117         
long l;
00118 } LH_PointerType;
00119 
static LH_PointerType PListe[2001];
00120 
static long PListeCount = 0;
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
void LH_mallocInit()
00134 {
00135         
long i;
00136         
for (i = 0; i < 2000; i++)
00137         {
00138                 PListe[i].p = 0;
00139                 PListe[i].l = 0;
00140         }
00141         PListeCount = 0;
00142 }
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
void* 
LH_malloc(
long a)
00157 {
00158         
long i;
00159 
#ifdef __MWERKS__
00160 
        void* aPtr = NewPtr(a);
00161 
#else
00162 
        void* aPtr = malloc(a);
00163 
#endif  
00164 
00165         
for (i = 0; i < PListeCount; i++)
00166         {
00167                 
if (aPtr < PListe[i].p)
00168                         
continue;
00169                 
if (aPtr >= (
char*)PListe[i].p + PListe[i].l)
00170                         
continue;
00171                 Debugger();
00172         }
00173 
00174         
for (i = 0; i < PListeCount; i++)
00175         {
00176                 
if (PListe[i].p == 0)
00177                         
break;
00178         }
00179         PListe[i].p = aPtr;
00180         PListe[i].l = a;
00181         
if (i >= PListeCount)
00182         {
00183                 
if (PListeCount < 2000)
00184                         PListeCount++;
00185         }
00186         
return aPtr;
00187 }
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
void LH_free(
void* a)
00202 {
00203         
long i;
00204         
for (i = 0; i < PListeCount; i++)
00205         {
00206                 
if (PListe[i].p == a)
00207                         
break;
00208         }
00209         
if (i < PListeCount)
00210         {
00211                 PListe[i].p = 0;
00212                 PListe[i].l = 0;
00213 
#ifdef __MWERKS__
00214 
                DisposePtr(a);
00215 
#else
00216 
                free(a);
00217 
#endif  
00218 
00219         }
00220         
else
00221         {
00222                 Debugger();
00223         }
00224 }
00225 
#else
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238 void LH_mallocInit()
00239 {
00240 }
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 void* 
LH_malloc(
long a)
00255 {
00256 
#ifdef __MWERKS__
00257 
        return NewPtr(a);
00258 
#else
00259 
        return malloc(a);
00260 
#endif  
00261 
00262 }
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 void LH_free(
void* a)
00277 {
00278 
#ifdef __MWERKS__
00279 
        DisposePtr((
Ptr)a);
00280 
#else
00281 
        free(a);
00282 
#endif  
00283 
00284 }
00285 
#endif  
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
void SetMem(
void* bytePtr,
00302                         size_t numBytes,
00303                         
unsigned char byteValue);
00304 
void SetMem(
void* bytePtr,
00305                         size_t numBytes,
00306                         
unsigned char byteValue)
00307 {
00308         memset(bytePtr, byteValue, numBytes);
00309 }
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
#if !__IS_MAC
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 void BlockMove(
const void* srcPtr,
00338                            
void* destPtr,
00339                            Size byteCount)
00340 {
00341         memmove(destPtr, srcPtr, byteCount);
00342 }
00343 
#endif
00344 
00345 
#ifdef IntelMode
00346 
00347 
00348 
00349 
00350 
00351 
00352 
00353 
00354 
00355 
00356 
00357 
00358 
00359 
void SwapLongOffset(
void* p,
00360                                         
unsigned long a,
00361                                         
unsigned long b)
00362 {
00363         
unsigned long* aPtr = (
unsigned long*)((
char*)p + a);
00364         
unsigned long* bPtr = (
unsigned long*)((
char*)p + b);
00365         
while (aPtr < bPtr)
00366         {
00367                 SwapLong(aPtr);
00368                 aPtr++;
00369         }
00370 }
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
void SwapShortOffset(
void* p,
00387                                          
unsigned long a,
00388                                          
unsigned long b);
00389 
void SwapShortOffset(
void* p,
00390                                          
unsigned long a,
00391                                          
unsigned long b)
00392 {
00393         
unsigned short* aPtr = (
unsigned short*)((
char*)p + a);
00394         
unsigned short* bPtr = (
unsigned short*)((
char*)p + b);
00395         
while (aPtr < bPtr)
00396         {
00397                 SwapShort(aPtr);
00398                 aPtr++;
00399         }
00400 }
00401 
00402 
#endif
00403 
00404