summaryrefslogtreecommitdiff
path: root/benchmarks/CUDA/MUM/PoolMalloc.cpp
blob: 8f315586a133fe59973a998390950f73337d7fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "PoolMalloc.hh"
//#include "CUtilities.h"
#include <iostream>
#include <cstring>
using namespace std;

static const size_t POOLBLOCKSIZE = 10*1024*1024;

/// Print current line and exit
#define DIE()                                                           \
  do {                                                                  \
    fprintf(stderr,"Aborted. File: %s, Line: %d\n",__FILE__,__LINE__);  \
    exit(EXIT_FAILURE);                                                 \
  } while(0)

/// Print a message and then DIE()
#define DIEM(m) do { fprintf(stderr,m"\n"); DIE(); } while(0)

/// DIE() if the pointer is NULL
#define DIENULL(p) do { if ( !(p) ) { DIEM("Out of memory"); } } while(0)


/// Malloc memory, DIE() if NULL
#define MALLOC(p,t,s) DIENULL( (p) = (t) malloc(s) )

/// Malloc memory for objects of type t, DIE() if NULL
#define NEW(p,t) MALLOC(p, t*, sizeof(t))


#include <cstdlib>

struct PoolNode_t
{
  char *block;
  size_t offset;
  size_t remaining;
  size_t size;
  
  PoolNode_t *next;
};



//============================================================ PoolMalloc_t ====
//------------------------------------------------------------ PoolMalloc_t ----
PoolMalloc_t::PoolMalloc_t()
{
  head_m = NULL;
}


//----------------------------------------------------------- ~PoolMalloc_t ----
PoolMalloc_t::~PoolMalloc_t()
{
  pfree();
}


//-------------------------------------------------------------------- free ----
void PoolMalloc_t::pfree()
{
  PoolNode_t * next;

  while ( head_m )
    {
      next = head_m->next;
      free(head_m->block);
      free(head_m);
      head_m = next;
    }

  head_m = NULL;
}


//------------------------------------------------------------------ malloc ----
///
/// Parcel memory from big byte buckets. Return all memory with 8 byte
/// alignment to silence the bus errors on Alpha and Solaris.
///
void * PoolMalloc_t::pmalloc(size_t size)
{
  size_t remainder = size % 8;

  //-- Make sure the next block is 8 byte aligned
  if ( remainder ) size += 8 - remainder;

  if ( head_m == NULL || size > head_m->remaining )
    {
      size_t blockSize = POOLBLOCKSIZE;
      if ( size > blockSize ) blockSize = size;

      PoolNode_t *newHead;
      NEW(newHead, PoolNode_t);
      MALLOC(newHead->block, char*, blockSize);
      newHead->size = blockSize;
      newHead->remaining = blockSize;
      newHead->offset = 0;
      newHead->next = head_m;
      head_m = newHead;
    }

  void *retval = head_m->block + head_m->offset;
  head_m->offset += size;
  head_m->remaining -= size;

  return retval;
}


//------------------------------------------------------------------ strdup ----
char * PoolMalloc_t::pstrdup(const char * s)
{
  size_t size = strlen(s) + 1;
  char *retval = (char *) pmalloc(size);
  memcpy(retval, s, size);
  return retval;
}