File: c-safe-memalloc.c

package info (click to toggle)
python-ltfatpy 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,412 kB
  • sloc: ansic: 8,546; python: 6,470; makefile: 15
file content (97 lines) | stat: -rw-r--r-- 1,511 bytes parent folder | download | duplicates (6)
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
#include "ltfat.h"



LTFAT_EXTERN_TOO
void* ltfat_malloc (size_t n)
{
    void *outp;
    outp = fftw_malloc(n);
    if (outp == NULL)
    {
        puts("ltfat_malloc failed.");
        exit(1);
    }

    return outp;
}

LTFAT_EXTERN_TOO
void* ltfat_realloc (void *ptr, size_t n)
{
    void *outp;
    // DOES NOT PRODUCE MEMORY ALIGNED POINTER
    // outp = realloc(ptr, n);
    outp = fftw_malloc(n);

    if (outp == NULL)
    {
        puts("ltfat_realloc failed.");
        exit(1);
    }

    if(ptr!=NULL)
    {
        ltfat_free(ptr);
    }

    return outp;
}

void* ltfat_realloc_and_copy (void *ptr, size_t nold, size_t nnew)
{
    if (ptr == NULL)
    {
        puts("Null pointer.");
        exit(1);
    }

    void *outp;

    outp = fftw_malloc(nnew);

    if (outp == NULL)
    {
        puts("ltfat_realloc_and_copy failed.");
        exit(1);
    }

    memcpy(outp,ptr,nold<nnew?nold:nnew);

    ltfat_free(ptr);

    return outp;
}

LTFAT_EXTERN_TOO
void* ltfat_calloc (size_t nmemb, size_t size)
{
    void *outp;
    // DOES NOT PRODUCE MEMORY ALIGNED POINTER
    // outp = calloc(nmemb, size);

    // workaround
    outp = fftw_malloc(nmemb*size);

    if (outp == NULL)
    {
        puts("ltfat_calloc failed.");
        exit(1);
    }
    // workaround
    memset(outp,0,nmemb*size);

    return outp;
}

LTFAT_EXTERN_TOO
void ltfat_free(const void *ptr)
{
    fftw_free((void*)ptr);
}

void ltfat_safefree(const void *ptr)
{
    if(ptr!=NULL)
        ltfat_free((void *)ptr);
}