-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssalloc.c
150 lines (137 loc) · 3.78 KB
/
ssalloc.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#include "ssalloc.h"
#include "measurements.h"
#if !defined(SSALLOC_USE_MALLOC)
static __thread void* ssalloc_app_mem[SSALLOC_NUM_ALLOCATORS];
static __thread size_t alloc_next[SSALLOC_NUM_ALLOCATORS] = {0};
static __thread void* ssalloc_free_list[SSALLOC_NUM_ALLOCATORS][256] = {{0}};
static __thread uint8_t ssalloc_free_cur[SSALLOC_NUM_ALLOCATORS] = {0};
static __thread uint8_t ssalloc_free_num[SSALLOC_NUM_ALLOCATORS] = {0};
#endif
void
ssalloc_set(void* mem)
{
#if !defined(SSALLOC_USE_MALLOC)
ssalloc_app_mem[0] = mem;
#endif
}
void
ssalloc_init()
{
#if !defined(SSALLOC_USE_MALLOC)
int i;
for (i = 0; i < SSALLOC_NUM_ALLOCATORS; i++)
{
ssalloc_app_mem[i] = (void*) memalign(64, SSALLOC_SIZE);
assert(ssalloc_app_mem[i] != NULL);
}
#endif
}
void
ssalloc_align()
{
#if !defined(SSALLOC_USE_MALLOC)
int i;
for (i = 0; i < SSALLOC_NUM_ALLOCATORS; i++)
{
while (alloc_next[i] % 64)
{
alloc_next[i]++;
}
}
#endif
}
void
ssalloc_align_alloc(unsigned int allocator)
{
#if !defined(SSALLOC_USE_MALLOC)
while (alloc_next[allocator] % 64)
{
alloc_next[allocator]++;
}
#endif
}
void
ssalloc_offset(size_t size)
{
#if !defined(SSALLOC_USE_MALLOC)
ssalloc_app_mem[0] += size;
#endif
}
//--------------------------------------------------------------------------------------
// FUNCTION: ssmalloc
//--------------------------------------------------------------------------------------
// Allocate memory in off-chip shared memory. This is a collective call that should be
// issued by all participating cores if consistent results are required. All cores will
// allocate space that is exactly overlapping. Alternatively, determine the beginning of
// the off-chip shared memory on all cores and subsequently let just one core do all the
// allocating and freeing. It can then pass offsets to other cores who need to know what
// shared memory regions were involved.
//--------------------------------------------------------------------------------------
// requested space
void*
ssalloc_alloc(unsigned int allocator, size_t size)
{
/* PF_START(1); */
void* ret = NULL;
#if defined(SSALLOC_USE_MALLOC)
ret = (void*) malloc(size);
#else
if (ssalloc_free_num[allocator] > 2)
{
uint8_t spot = ssalloc_free_cur[allocator] - ssalloc_free_num[allocator];
ret = ssalloc_free_list[allocator][spot];
ssalloc_free_num[allocator]--;
}
else
{
ret = ssalloc_app_mem[allocator] + alloc_next[allocator];
alloc_next[allocator] += size;
if (alloc_next[allocator] > SSALLOC_SIZE)
{
fprintf(stderr, "*** warning: allocator %2d : out of bounds alloc\n", allocator);
}
}
#endif
/* PRINT("[lib] allocated %p [offs: %lu]", ret, ssalloc_app_addr_offs(ret)); */
/* PF_STOP(1); */
return ret;
}
void*
ssalloc(size_t size)
{
return ssalloc_alloc(0, size);
}
//--------------------------------------------------------------------------------------
// FUNCTION: ssfree
//--------------------------------------------------------------------------------------
// Deallocate memory in shared memory. Also collective, see ssmalloc
//--------------------------------------------------------------------------------------
// pointer to data to be freed
void
ssfree_alloc(unsigned int allocator, void* ptr)
{
#if defined(SSALLOC_USE_MALLOC)
free(ptr);
#else
ssalloc_free_num[allocator]++;
/* PRINT("free %3d (num_free after: %3d)", ssalloc_free_cur, ssalloc_free_num); */
ssalloc_free_list[allocator][ssalloc_free_cur[allocator]++] = ptr;
#endif
}
void
ssfree(void* ptr)
{
ssfree_alloc(0, ptr);
}