File: PsychMemory.c

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (253 lines) | stat: -rw-r--r-- 6,827 bytes parent folder | download | duplicates (4)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
    Psychtoolbox3/Source/Common/PsychMemory.c

    AUTHORS:

    Allen.Ingling@nyu.edu           awi
    mario.kleiner.de@gmail.com      mk

    PLATFORMS: All

    PROJECTS: All

    HISTORY:

    09/04/02  awi   Wrote it.
    03/19/11  mk    Make 64-bit clean.

*/

#include "Psych.h"

// Pointer to the start of our list of allocated temporary memory buffers:
static size_t* PsychTempMemHead = NULL;

// Total count of allocated memory in Bytes:
static size_t totalTempMemAllocated = 0;

// Convert a double value (which encodes a memory address) into a ptr:
void*  PsychDoubleToPtr(volatile double dptr)
{
    volatile psych_uint64* iptr = (psych_uint64*) &dptr;
    volatile psych_uint64 ival = *iptr;
    return((void*) ((size_t) ival));
}

// Convert a memory address pointer into a double value:
double PsychPtrToDouble(void* ptr)
{
    volatile psych_uint64 ival = (psych_uint64) ((size_t) ptr);
    volatile double* dptr = (double*) &ival;
    volatile double outval = *dptr;
    return(outval);
}

#if PSYCH_LANGUAGE == PSYCH_MATLAB

// If running on Matlab, we use Matlab's memory manager...
void *PsychCallocTemp(size_t n, size_t size)
{
    void *ret;

    if(NULL==(ret=mxCalloc(n, size))){
        if(size * n != 0)
        PsychErrorExitMsg(PsychError_outofMemory, NULL);
    }

    return(ret);
}

void *PsychMallocTemp(size_t n)
{
    void *ret;

    if(NULL==(ret=mxMalloc(n))){
        if(n!=0)
        PsychErrorExitMsg(PsychError_outofMemory,NULL);
    }

    return(ret);
}

#else

// If not running on Matlab, we use our own allocator...

#define PTBTEMPMEMDEC(n) totalTempMemAllocated -=(n)

// Enqueues a new record into our linked list of temp. memory buffers.
// Returns the memory pointer to be passed to rest of Psychtoolbox.
static void* PsychEnqueueTempMemory(size_t* p, size_t n)
{
    // Add current buffer-head ptr as next-pointer to our new buffer:
    *p = (size_t) PsychTempMemHead;

    // Set our buffer as new head of list:
    PsychTempMemHead = p;

    // Add allocated buffer size as 2nd element:
    p++;
    *p = n;

    // Accounting:
    totalTempMemAllocated += n;
    // printf("TEMPMALLOCED %i Bytes,  new = %i.\n", n, totalTempMemAllocated); fflush(NULL);

    // Increment p again to get real start of user-visible buffer:
    p++;

    // Return ptr:
    return((void*) p);
}

void *PsychCallocTemp(size_t n, size_t size)
{
    size_t *ret;

    // MK: This could create an overflow if product n * size is
    // bigger than length of a unsigned long int --> Only
    // happens if more than 4 GB of RAM are allocated at once.
    // --> Improbable for PTB, unless someones trying a buffer
    // overflow attack -- PTB would lose there badly anyway...
    size_t realsize = n * size + sizeof(void*) + sizeof(size_t);

    // realsize has extra bytes allocated for our little header...  
    if (NULL == (ret = (size_t*) calloc((size_t) 1, realsize))) {
        PsychErrorExitMsg(PsychError_outofMemory, NULL);
    }

    // Need to enqueue memory buffer...
    return(PsychEnqueueTempMemory(ret, realsize));
}

void *PsychMallocTemp(size_t n)
{
    size_t *ret;

    // Allocate some extra bytes for our little header...
    n = n + sizeof(void*) + sizeof(size_t);

    if (NULL == (ret = (size_t*) malloc(n))) {
        PsychErrorExitMsg(PsychError_outofMemory,NULL);
    }

    // Need to enqueue memory buffer...
    return(PsychEnqueueTempMemory(ret, n));
}

// Free a single spec'd temp memory buffer.
// TODO Note: The current implementation of our allocator
// uses a single-linked list, which has O(1) cost for
// allocating memory (Optimal!) and O(n) cost for freeing
// all allocated memory (Optimal!), but it has up to
// O(n) cost for deleting a single memory buffer as well,
// be n the total number of allocated buffers. This is
// worst-case upper bound. If PsychFreeTemp() is used a
// lot on long buffer lists, this will incur significant
// overhead! A better implementation would use a double-
// linked list or even a binary tree or hash structure,
// but for now this has to be good enough(TM).
void PsychFreeTemp(void* inptr)
{
    size_t *ptr = (size_t*) inptr;
    size_t *ptrbackup = ptr;
    size_t *next = PsychTempMemHead;
    size_t *prevptr = NULL;

    if (ptr == NULL)
        return;

    // Convert ptb supplied pointer (in)ptr into real start
    // of our buffer, including our header:
    ptr-= 2;

    if (ptr == NULL)
        return;

    if (PsychTempMemHead == ptr) {
        // Special case: ptr is first buffer in queue. Dequeue:
        PsychTempMemHead = (size_t*) *PsychTempMemHead;

        // Some accounting:
        PTBTEMPMEMDEC(ptr[1]);

        // Release it:
        free(ptr);

        return;
    }

    // ptr valid and not first buffer in queue.
    // Walk the whole buffer list until we encounter our buffer:
    while (next != NULL && next != ptr) {
        prevptr = next;
        next = (size_t*) *next;
    }

    // Done with search loop. Did we find our buffer?
    if (next == ptr) {
        // Found it! Set next-ptr of previous buffer to next-ptr
        // of this buffer to dequeue from list:
        *prevptr = *next;

        // Some accounting:
        PTBTEMPMEMDEC(next[1]);

        // Release:
        free(ptr);

        // Done.
        return;
    }

    // Oops.: Did not find matching buffer to pointer --> Trouble!
    printf("PTB-BUG: In PsychFreeTemp: Tried to free non-existent temporary membuffer %p!!! Ignored.\n", ptrbackup);
    fflush(NULL);
    return;
}

// Master cleanup routine: Frees all allocated memory:
void PsychFreeAllTempMemory(void)
{
    size_t* p = NULL;
    size_t* next = PsychTempMemHead;

    // Walk our whole buffer list and release all buffers on it:
    while (next != NULL) {
        // next points to current buffer to release. Make a copy of
        // next:
        p = next;

        // Update next to point to the next buffer to release:
        next = (size_t*) *p;

        // Some accounting:
        PTBTEMPMEMDEC(p[1]);

        // Release buffer p:
        free(p);

        // We're done with this buffer, next points to next one to release
        // or is NULL if all released...
    }

    // Done. NULL-out the list start ptr:
    PsychTempMemHead = NULL;

    // Sanity check:
    if (totalTempMemAllocated != 0) {
        // Cannot use PsychErrorXXX Routines here, because this is outside
        // the jumpbuffer context for our error-routines. Could lead to
        // infinite recursion!!!
        printf("PTB-CRITICAL BUG: Inconsistency detected in temporary memory allocator!\n");
        printf("PTB-CRITICAL BUG: totalTempMemAllocated = %li after PsychFreeAllTempMemory()!!!!\n", totalTempMemAllocated);
        fflush(NULL);

        // Reset to defined state.
        totalTempMemAllocated = 0;
    }

    return;
}

#endif