/src/FreeImage/Source/LibRawLite/libraw/libraw_alloc.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 99 lines · 70 code · 9 blank · 20 comment · 9 complexity · 7722512ea89c7ee2afd6fa00a83a2241 MD5 · raw file

  1. /* -*- C++ -*-
  2. * File: libraw_alloc.h
  3. * Copyright 2008-2010 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. 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  11. (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  12. 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  13. (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
  14. 3. LibRaw Software License 27032010
  15. (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
  16. */
  17. #ifndef __LIBRAW_ALLOC_H
  18. #define __LIBRAW_ALLOC_H
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef __cplusplus
  22. #define MSIZE 32
  23. class DllDef libraw_memmgr
  24. {
  25. public:
  26. libraw_memmgr()
  27. {
  28. memset(mems,0,sizeof(mems));
  29. calloc_cnt=0;
  30. }
  31. void *malloc(size_t sz)
  32. {
  33. void *ptr = ::malloc(sz);
  34. mem_ptr(ptr);
  35. return ptr;
  36. }
  37. void *calloc(size_t n, size_t sz)
  38. {
  39. void *ptr = ::calloc(n,sz);
  40. mem_ptr(ptr);
  41. return ptr;
  42. }
  43. void *realloc(void *ptr,size_t newsz)
  44. {
  45. void *ret = ::realloc(ptr,newsz);
  46. forget_ptr(ptr);
  47. mem_ptr(ret);
  48. return ret;
  49. }
  50. void free(void *ptr)
  51. {
  52. forget_ptr(ptr);
  53. ::free(ptr);
  54. }
  55. void cleanup(void)
  56. {
  57. for(int i = 0; i< MSIZE; i++)
  58. if(mems[i])
  59. {
  60. free(mems[i]);
  61. mems[i] = NULL;
  62. }
  63. }
  64. private:
  65. void *mems[MSIZE];
  66. int calloc_cnt;
  67. void mem_ptr(void *ptr)
  68. {
  69. if(ptr)
  70. for(int i=0;i < MSIZE; i++)
  71. if(!mems[i])
  72. {
  73. mems[i] = ptr;
  74. break;
  75. }
  76. }
  77. void forget_ptr(void *ptr)
  78. {
  79. if(ptr)
  80. for(int i=0;i < MSIZE; i++)
  81. if(mems[i] == ptr)
  82. mems[i] = NULL;
  83. }
  84. };
  85. #endif /* C++ */
  86. #endif