contrib/OWB: add correct SDL dependency, fix compilers used
[AROS-Contrib.git] / freetype1 / contrib / ftos2 / lib / ttmemory.c
blob38fd97948f75b4b84027b9bcfc02e99dd5ed6fa6
1 /*******************************************************************
3 * ttmemory.c 1.2
5 * Memory management component (body).
7 * Copyright 1996, 1997 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * Portions Copyright 1998 by Michal Necasek
12 * This file is part of the FreeType project, and may only be used
13 * modified and distributed under the terms of the FreeType project
14 * license, LICENSE.TXT. By continuing to use, modify, or distribute
15 * this file you indicate that you have read the license and
16 * understand and accept it fully.
19 * Changes between 1.1 and 1.2:
21 * - the font pool is gone.
23 * - introduced the FREE macro and the Free function for
24 * future use in destructors.
26 * - Init_FontPool() is now a macro to allow the compilation of
27 * 'legacy' applications (all four test programs have been updated).
29 * Note: This code was slightly adapted for use in the OS/2
30 * Font Driver (FreeType/2).
32 ******************************************************************/
34 #include "ttdebug.h"
35 #include "ttmemory.h"
36 #include "ttengine.h"
38 #define INCL_DEV
39 #include <os2.h>
40 #include <pmddi.h>
42 #include <stdlib.h>
44 #undef DEBUG_MEM
46 /* -------------------- debugging defs ----------------------- */
47 /* DEBUG_MEM creates a file and logs all actions to it */
49 #ifdef DEBUG_MEM
50 static HFILE MemLogHandle = NULLHANDLE;
51 static ULONG Written = 0;
52 static char log[2048] = "";
53 static char buf[2048] = "";
56 char* itoa10( int i, char* buffer ) {
57 char* ptr = buffer;
58 char* rptr = buffer;
59 char digit;
61 if (i == 0) {
62 buffer[0] = '0';
63 buffer[1] = 0;
64 return buffer;
67 if (i < 0) {
68 *ptr = '-';
69 ptr++; rptr++;
70 i = -i;
73 while (i != 0) {
74 *ptr = (char) (i % 10 + '0');
75 ptr++;
76 i /= 10;
79 *ptr = 0; ptr--;
81 while (ptr > rptr) {
82 digit = *ptr;
83 *ptr = *rptr;
84 *rptr = digit;
85 ptr--;
86 rptr++;
89 return buffer;
92 static const char* hexstr = "0123456789abcdef";
94 char* itohex2( int i, char* buffer )
96 buffer[0] = hexstr[ (i >> 12) & 0xF ];
97 buffer[1] = hexstr[ (i >> 8 ) & 0xF ];
98 buffer[2] = hexstr[ (i >> 4 ) & 0xF ];
99 buffer[3] = hexstr[ (i ) & 0xF ];
100 buffer[4] = '\0';
101 return buffer;
104 char* itohex4( long i, char* buffer )
106 itohex2( (i >> 16) & 0xFFFF, buffer );
107 /* We separate the high and low part with a dot to make it */
108 /* more readable */
109 buffer[4] = '.';
110 itohex2( i & 0xFFFF, buffer+5 );
111 return buffer;
114 #define COPY(s) strcpy(log, s)
115 #define CAT(s) strcat(log, s)
116 #define CATI(v) strcat(log, itoa10( (int)v, buf ))
117 #define CATH(v) strcat(log, itohex4( (long)v, buf ))
118 #define CATW(v) strcat(log, itohex2( (short)v, buf ))
119 #define WRITE DosWrite(MemLogHandle, log, strlen(log), &Written)
120 #define ERRRET(e) { COPY("Error at "); \
121 CATI(__LINE__); \
122 CAT("\r\n"); \
123 WRITE; \
124 return(e); \
127 #else
129 #define COPY(s)
130 #define CAT(s)
131 #define CATI(v)
132 #define CATH(v)
133 #define CATW(v)
134 #define WRITE
135 #define ERRRET(e) return(e);
137 #endif /* DEBUG_MEM */
140 #undef TRACK_MEM
141 /* TRACK_MEM allows online tracking of memory usage online (via shared */
142 /* memory). It is used in conjunction with the FTMEM utility. */
144 #ifdef TRACK_MEM
145 /* name of shared memory used for memory usage reporting */
146 #define MEM_NAME "\\sharemem\\freetype"
148 /* structure containing memory usage information */
149 typedef struct _INFOSTRUCT {
150 ULONG signature; /* signature (0x46524545, 'FREE') */
151 ULONG used; /* bytes actually used */
152 ULONG maxused; /* maximum amount ever used */
153 ULONG num_err; /* number of (de)allocation errors */
154 } INFOSTRUCT, *PINFOSTRUCT;
156 /* structure (in named shared memory) pointing to the above struct */
157 typedef struct _INFOPTR {
158 PINFOSTRUCT address; /* pointer to actual memory info */
159 } INFOPTR, *PINFOPTR;
161 PINFOSTRUCT meminfo; /* struct in shared memory holding usage info */
162 PINFOPTR memptr;
163 #endif
165 /* -----------------------------------------------------------------
167 A brief explanation of the memory allocator :
169 - We store the block's size in front of it. The size implies the nature
170 of the block, and selects a de-allocation scheme..
172 - A note on the memory debugging schemes: logging and online tracking
173 are independent of each other and none, either or both may be used.
175 ----------------------------------------------------------------- */
177 /****************************************************************/
178 /* */
179 /* Allocate a block of memory */
180 /* */
181 static
182 void* ft2_malloc( long size )
184 long* head;
185 void* base;
186 int rc;
188 /* add header size */
189 size += sizeof(long);
191 /* Allocate memory accessible from all processes */
192 if (( rc = SSAllocMem( (PVOID)&head, size, 0 )))
194 COPY( "ft2_malloc: block SSAllocMem failed with rc = " );
195 CATH( rc );
196 CAT ( "\r\n" );
197 WRITE;
198 return NULL;
200 *head = size;
201 base = (void*)(head + 1);
202 #ifdef TRACK_MEM
203 meminfo->used += size;
204 if (meminfo->used > meminfo->maxused)
205 meminfo->maxused = meminfo->used;
206 #endif
207 return base;
210 /****************************************************************/
211 /* */
212 /* Release a block of memory */
213 /* */
214 int ft2_free( void* block )
216 long* head;
217 long size, offset;
218 int rc, h;
220 if (!block)
221 return -1;
223 head = ((long*)block) - 1;
224 size = *head;
226 if (size <= 0)
228 COPY( "ft2_free: negative size !!\r\n" );
229 WRITE;
230 return -1;
233 rc = SSFreeMem( (PVOID)head );
234 if (rc)
236 COPY( "ft2_free: block SSFreeMem failed with rc = " );
237 CATI( rc );
238 CAT ( "\r\n" );
239 WRITE;
241 #ifdef TRACK_MEM
242 meminfo->used -= size;
243 #endif
244 return rc;
247 /*******************************************************************
249 * Function : TT_Alloc
251 * Description : Allocates memory from the heap buffer.
253 * Input : Size size of the memory to be allocated
254 * P pointer to a buffer pointer
256 * Output : Error code.
258 * NOTE: The newly allocated block should _always_ be zeroed
259 * on return. Many parts of the engine rely on this to
260 * work properly.
262 ******************************************************************/
264 TT_Error TT_Alloc( long Size, void** P )
266 if ( Size )
268 *P = ft2_malloc( Size );
269 if (!*P) {
270 #ifdef TRACK_MEM
271 meminfo->num_err++;
272 #endif
273 return TT_Err_Out_Of_Memory;
276 /* MEM_Set( *P, 0, Size); */ /* not necessary, SSAllocMem does it */
278 else
279 *P = NULL;
281 return TT_Err_Ok;
285 /*******************************************************************
287 * Function : TT_Free
289 * Description : Releases a previously allocated block of memory.
291 * Input : P pointer to memory block
293 * Output : Always SUCCESS.
295 * Note : The pointer must _always_ be set to NULL by this function.
297 ******************************************************************/
299 TT_Error TT_Free( void** P )
301 if ( !P || !*P )
302 return TT_Err_Ok;
304 if (ft2_free( *P )) {
305 #ifdef TRACK_MEM
306 meminfo->num_err++;
307 #endif
309 *P = NULL;
310 return TT_Err_Ok;
314 /*******************************************************************
316 * Function : TTMemory_Init
318 * Description : Initializes the memory.
320 * Output : Always SUCCESS.
322 ******************************************************************/
324 TT_Error TTMemory_Init()
326 int rc;
328 #ifdef DEBUG_MEM
329 ULONG Action;
331 DosOpen("C:\\FTMEM.LOG", &MemLogHandle, &Action, 0, FILE_NORMAL,
332 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
333 OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_WRITE_THROUGH |
334 OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_WRITEONLY,
335 NULL);
337 COPY("FTMEM Init.\r\n");
338 WRITE;
340 #endif /* DEBUG */
342 #ifdef TRACK_MEM
343 /* allocate named shared memory and global shared memory */
345 SSAllocMem(&meminfo, 4096, 0);
346 DosAllocSharedMem((PVOID*)&memptr, MEM_NAME, 4096, fALLOC);
347 memptr->address = meminfo;
348 meminfo->signature = 0x46524545; /* 'FREE' */
349 meminfo->maxused = 0;
350 meminfo->used = 0;
351 #endif /* TRACK */
353 return TT_Err_Ok;
357 /*******************************************************************
359 * Function : TTMemory_Done
361 * Description : Finalizes memory usage.
363 * Output : Always SUCCESS.
365 ******************************************************************/
367 TT_Error TTMemory_Done()
369 /* Never called by the font driver (beats me why). We do not
370 release the heaps */
372 #ifdef TRACK_MEM
373 DosFreeMem(memptr); /* free shared memory */
374 SSFreeMem(meminfo);
375 #endif
376 #ifdef DEBUG_MEM
377 COPY("FTMEM Done.\r\n");
378 WRITE;
379 DosClose(MemLogHandle);
380 #endif
381 return TT_Err_Ok;
385 /* END */