1 | /* -*- C++ -*- |
2 | * File: libraw_alloc.h |
3 | * Copyright 2008-2013 LibRaw LLC (info@libraw.org) |
4 | * Created: Sat Mar 22, 2008 |
5 | * |
6 | * LibRaw C++ interface |
7 | * |
8 | LibRaw is free software; you can redistribute it and/or modify |
9 | it under the terms of the one of three licenses as you choose: |
10 | |
11 | 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1 |
12 | (See file LICENSE.LGPL provided in LibRaw distribution archive for details). |
13 | |
14 | 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 |
15 | (See file LICENSE.CDDL provided in LibRaw distribution archive for details). |
16 | |
17 | 3. LibRaw Software License 27032010 |
18 | (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details). |
19 | |
20 | */ |
21 | |
22 | #ifndef __LIBRAW_ALLOC_H |
23 | #define __LIBRAW_ALLOC_H |
24 | |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | |
28 | #ifdef __cplusplus |
29 | |
30 | #define LIBRAW_MSIZE 32 |
31 | |
32 | class DllDef libraw_memmgr |
33 | { |
34 | public: |
35 | libraw_memmgr() |
36 | { |
37 | memset(mems,0,sizeof(mems)); |
38 | calloc_cnt=0; |
39 | } |
40 | void *malloc(size_t sz) |
41 | { |
42 | void *ptr = ::malloc(sz); |
43 | mem_ptr(ptr); |
44 | return ptr; |
45 | } |
46 | void *calloc(size_t n, size_t sz) |
47 | { |
48 | void *ptr = ::calloc(n,sz); |
49 | mem_ptr(ptr); |
50 | return ptr; |
51 | } |
52 | void *realloc(void *ptr,size_t newsz) |
53 | { |
54 | void *ret = ::realloc(ptr,newsz); |
55 | forget_ptr(ptr); |
56 | mem_ptr(ret); |
57 | return ret; |
58 | } |
59 | void free(void *ptr) |
60 | { |
61 | forget_ptr(ptr); |
62 | ::free(ptr); |
63 | } |
64 | void cleanup(void) |
65 | { |
66 | for(int i = 0; i< LIBRAW_MSIZE; i++) |
67 | if(mems[i]) |
68 | { |
69 | free(mems[i]); |
70 | mems[i] = NULL; |
71 | } |
72 | } |
73 | |
74 | private: |
75 | void *mems[LIBRAW_MSIZE]; |
76 | int calloc_cnt; |
77 | void mem_ptr(void *ptr) |
78 | { |
79 | if(ptr) |
80 | for(int i=0;i < LIBRAW_MSIZE; i++) |
81 | if(!mems[i]) |
82 | { |
83 | mems[i] = ptr; |
84 | break; |
85 | } |
86 | } |
87 | void forget_ptr(void *ptr) |
88 | { |
89 | if(ptr) |
90 | for(int i=0;i < LIBRAW_MSIZE; i++) |
91 | if(mems[i] == ptr) |
92 | mems[i] = NULL; |
93 | } |
94 | |
95 | }; |
96 | |
97 | #endif /* C++ */ |
98 | |
99 | #endif |
100 | |