Advertisement
Guest User

Chris M Thomasson

a guest
May 29th, 2009
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 KB | None | 0 0
  1. #if ! defined (RALLOC_H)
  2. #  define RALLOC_H
  3. #  if defined (__cplusplus)
  4.      extern "C" {
  5. #  endif
  6. /**************************************************************/
  7.  
  8.  
  9.  
  10.  
  11. #include <stddef.h>
  12. #include <assert.h>
  13.  
  14.  
  15.  
  16.  
  17. #if defined (_MSC_VER)
  18. /* warning C4116: unnamed type definition in parentheses */
  19. #  pragma warning (disable : 4116)
  20. #endif
  21.  
  22.  
  23.  
  24.  
  25. #if ! defined (NDEBUG)
  26. #  include <stdio.h>
  27. #  define RALLOC_DBG_PRINTF(mp_exp) printf mp_exp
  28. #else
  29. #  define RALLOC_DBG_PRINTF(mp_exp) ((void)0)
  30. #endif
  31.  
  32.  
  33.  
  34.  
  35. #if ! defined (RALLOC_UINTPTR_TYPE)
  36. #  define RALLOC_UINTPTR_TYPE size_t
  37. #endif
  38.  
  39.  
  40.  
  41.  
  42. typedef RALLOC_UINTPTR_TYPE ralloc_uintptr_type;
  43.  
  44.  
  45. typedef char ralloc_static_assert[
  46.   sizeof(ralloc_uintptr_type) == sizeof(void*) ? 1 : -1
  47. ];
  48.  
  49.  
  50.  
  51.  
  52. enum ralloc_align_enum {
  53.   ALIGN_ENUM
  54. };
  55.  
  56.  
  57. struct ralloc_align_struct {
  58.   char pad;
  59.   double type;
  60. };
  61.  
  62.  
  63. union ralloc_align_max {
  64.   char char_;
  65.   short int short_;
  66.   int int_;
  67.   long int long_;
  68.   float float_;
  69.   double double_;
  70.   long double long_double_;
  71.   void* ptr_;
  72.   void* (*fptr_) (void*);
  73.   enum ralloc_align_enum enum_;
  74.   struct ralloc_align_struct struct_;
  75.   size_t size_t_;
  76.   ptrdiff_t ptrdiff_t;
  77. };
  78.  
  79.  
  80. #define RALLOC_ALIGN_OF(mp_type) \
  81.   offsetof( \
  82.     struct { \
  83.       char pad_RALLOC_ALIGN_OF; \
  84.       mp_type type_RALLOC_ALIGN_OF; \
  85.     }, \
  86.     type_RALLOC_ALIGN_OF \
  87.   )
  88.  
  89.  
  90. #define RALLOC_ALIGN_MAX RALLOC_ALIGN_OF(union ralloc_align_max)
  91.  
  92.  
  93. #define RALLOC_ALIGN_UP(mp_ptr, mp_align) \
  94.   ((void*)( \
  95.     (((ralloc_uintptr_type)(mp_ptr)) + ((mp_align) - 1)) \
  96.     & ~(((mp_align) - 1)) \
  97.   ))
  98.  
  99.  
  100. #define RALLOC_ALIGN_ASSERT(mp_ptr, mp_align) \
  101.   (((void*)(mp_ptr)) == RALLOC_ALIGN_UP(mp_ptr, mp_align))
  102.  
  103.  
  104.  
  105.  
  106. struct region {
  107.   unsigned char* buffer;
  108.   size_t size;
  109.   size_t offset;
  110. };
  111.  
  112.  
  113. static void
  114. rinit(
  115.  struct region* const self,
  116.  void* buffer,
  117.  size_t size
  118. ) {
  119.   self->buffer = buffer;
  120.   self->size = size;
  121.   self->offset = 0;
  122.  
  123.   RALLOC_DBG_PRINTF((
  124.     "rinit(%p) {\n"
  125.     "  buffer          = %p\n"
  126.     "  size            = %lu\n"
  127.     "}\n\n\n",
  128.     (void*)self,
  129.     buffer,
  130.     (unsigned long int)size
  131.   ));
  132. }
  133.  
  134.  
  135. static void*
  136. rallocex(
  137.  struct region* const self,
  138.  size_t size,
  139.  size_t align
  140. ) {
  141.   unsigned char* align_buffer;
  142.   size_t offset = self->offset;
  143.   unsigned char* raw_buffer = self->buffer + offset;
  144.  
  145.   if (! size) {
  146.     size = 1;
  147.   }
  148.  
  149.   if (! align) {
  150.     align = RALLOC_ALIGN_MAX;
  151.   }
  152.  
  153.   assert(align == 1 || RALLOC_ALIGN_ASSERT(align, 2));
  154.  
  155.   align_buffer = RALLOC_ALIGN_UP(raw_buffer, align);
  156.  
  157.   assert(RALLOC_ALIGN_ASSERT(align_buffer, align));
  158.  
  159.   size += align_buffer - raw_buffer;
  160.  
  161.   if (offset + size > self->size) {
  162.     return NULL;
  163.   }
  164.  
  165.   self->offset = offset + size;
  166.  
  167.   RALLOC_DBG_PRINTF((
  168.     "rallocex(%p) {\n"
  169.     "  size            = %lu\n"
  170.     "  alignment       = %lu\n"
  171.     "  origin offset   = %lu\n"
  172.     "  final offset    = %lu\n"
  173.     "  raw_buffer      = %p\n"
  174.     "  align_buffer    = %p\n"
  175.     "  size adjustment = %lu\n"
  176.     "  final size      = %lu\n"
  177.     "}\n\n\n",
  178.     (void*)self,
  179.     (unsigned long int)size - (align_buffer - raw_buffer),
  180.     (unsigned long int)align,
  181.     (unsigned long int)offset,
  182.     (unsigned long int)self->offset,
  183.     (void*)raw_buffer,
  184.     (void*)align_buffer,
  185.     (unsigned long int)(align_buffer - raw_buffer),
  186.     (unsigned long int)size
  187.   ));
  188.  
  189.   return align_buffer;
  190. }
  191.  
  192.  
  193. #define ralloc(mp_self, mp_size) \
  194.   rallocex((mp_self), (mp_size), RALLOC_ALIGN_MAX)
  195.  
  196. #define ralloct(mp_self, mp_count, mp_type) \
  197.   rallocex( \
  198.     (mp_self), \
  199.     sizeof(mp_type) * (mp_count),\
  200.     RALLOC_ALIGN_OF(mp_type) \
  201.   )
  202.  
  203.  
  204. static void
  205. rflush(
  206.  struct region* const self
  207. ) {
  208.   self->offset = 0;
  209.  
  210.   RALLOC_DBG_PRINTF((
  211.     "rflush(%p) {}\n\n\n",
  212.     (void*)self
  213.   ));  
  214. }
  215.  
  216.  
  217.  
  218.  
  219. #undef RALLOC_DBG_PRINTF
  220. #undef RALLOC_UINTPTR_TYPE
  221.  
  222.  
  223.  
  224.  
  225. /**************************************************************/
  226. #  if defined (__cplusplus)
  227.      }
  228. #  endif
  229. #endif
  230.  
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement