equal
deleted
inserted
replaced
28 // file: |
28 // file: |
29 // |
29 // |
30 //--------------------------------------------------------------------------------- |
30 //--------------------------------------------------------------------------------- |
31 // |
31 // |
32 // Little Color Management System |
32 // Little Color Management System |
33 // Copyright (c) 1998-2010 Marti Maria Saguer |
33 // Copyright (c) 1998-2012 Marti Maria Saguer |
34 // |
34 // |
35 // Permission is hereby granted, free of charge, to any person obtaining |
35 // Permission is hereby granted, free of charge, to any person obtaining |
36 // a copy of this software and associated documentation files (the "Software"), |
36 // a copy of this software and associated documentation files (the "Software"), |
37 // to deal in the Software without restriction, including without limitation |
37 // to deal in the Software without restriction, including without limitation |
38 // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
38 // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
70 } |
70 } |
71 |
71 |
72 // long int because C99 specifies ftell in such way (7.19.9.2) |
72 // long int because C99 specifies ftell in such way (7.19.9.2) |
73 long int CMSEXPORT cmsfilelength(FILE* f) |
73 long int CMSEXPORT cmsfilelength(FILE* f) |
74 { |
74 { |
75 long int n; |
75 long int p , n; |
|
76 |
|
77 p = ftell(f); // register current file position |
76 |
78 |
77 if (fseek(f, 0, SEEK_END) != 0) { |
79 if (fseek(f, 0, SEEK_END) != 0) { |
78 return -1; |
80 return -1; |
79 } |
81 } |
|
82 |
80 n = ftell(f); |
83 n = ftell(f); |
81 fseek(f, 0, SEEK_SET); |
84 fseek(f, p, SEEK_SET); // file position restored |
82 |
85 |
83 return n; |
86 return n; |
84 } |
87 } |
|
88 |
85 |
89 |
86 // Memory handling ------------------------------------------------------------------ |
90 // Memory handling ------------------------------------------------------------------ |
87 // |
91 // |
88 // This is the interface to low-level memory management routines. By default a simple |
92 // This is the interface to low-level memory management routines. By default a simple |
89 // wrapping to malloc/free/realloc is provided, although there is a limit on the max |
93 // wrapping to malloc/free/realloc is provided, although there is a limit on the max |
157 // all memory is initialized to zero. |
161 // all memory is initialized to zero. |
158 static |
162 static |
159 void* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size) |
163 void* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size) |
160 { |
164 { |
161 cmsUInt32Number Total = num * size; |
165 cmsUInt32Number Total = num * size; |
|
166 |
|
167 // Preserve calloc behaviour |
|
168 if (Total == 0) return NULL; |
|
169 |
|
170 // Safe check for overflow. |
|
171 if (num >= UINT_MAX / size) return NULL; |
162 |
172 |
163 // Check for overflow |
173 // Check for overflow |
164 if (Total < num || Total < size) { |
174 if (Total < num || Total < size) { |
165 return NULL; |
175 return NULL; |
166 } |
176 } |
267 // ******************************************************************************************** |
277 // ******************************************************************************************** |
268 |
278 |
269 // Sub allocation takes care of many pointers of small size. The memory allocated in |
279 // Sub allocation takes care of many pointers of small size. The memory allocated in |
270 // this way have be freed at once. Next function allocates a single chunk for linked list |
280 // this way have be freed at once. Next function allocates a single chunk for linked list |
271 // I prefer this method over realloc due to the big inpact on xput realloc may have if |
281 // I prefer this method over realloc due to the big inpact on xput realloc may have if |
272 // memory is being swapped to disk. This approach is safer (although thats not true on any platform) |
282 // memory is being swapped to disk. This approach is safer (although that may not be true on all platforms) |
273 static |
283 static |
274 _cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial) |
284 _cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial) |
275 { |
285 { |
276 _cmsSubAllocator_chunk* chunk; |
286 _cmsSubAllocator_chunk* chunk; |
|
287 |
|
288 // 20K by default |
|
289 if (Initial == 0) |
|
290 Initial = 20*1024; |
277 |
291 |
278 // Create the container |
292 // Create the container |
279 chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk)); |
293 chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk)); |
280 if (chunk == NULL) return NULL; |
294 if (chunk == NULL) return NULL; |
281 |
295 |
286 // Something went wrong |
300 // Something went wrong |
287 _cmsFree(ContextID, chunk); |
301 _cmsFree(ContextID, chunk); |
288 return NULL; |
302 return NULL; |
289 } |
303 } |
290 |
304 |
291 // 20K by default |
305 |
292 if (Initial == 0) |
|
293 Initial = 20*1024; |
|
294 |
306 |
295 chunk ->BlockSize = Initial; |
307 chunk ->BlockSize = Initial; |
296 chunk ->Used = 0; |
308 chunk ->Used = 0; |
297 chunk ->next = NULL; |
309 chunk ->next = NULL; |
298 |
310 |
342 void* _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size) |
354 void* _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size) |
343 { |
355 { |
344 cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used; |
356 cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used; |
345 cmsUInt8Number* ptr; |
357 cmsUInt8Number* ptr; |
346 |
358 |
347 size = _cmsALIGNLONG(size); |
359 size = _cmsALIGNMEM(size); |
348 |
360 |
349 // Check for memory. If there is no room, allocate a new chunk of double memory size. |
361 // Check for memory. If there is no room, allocate a new chunk of double memory size. |
350 if (size > Free) { |
362 if (size > Free) { |
351 |
363 |
352 _cmsSubAllocator_chunk* chunk; |
364 _cmsSubAllocator_chunk* chunk; |