Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unificate malloc/free/currentTimeMicros functions. #555

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/arch/helperrvv.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ typedef vfloat64m1x4_t tdi_t;
// The configuration didn't provide a constant vector length, meaning it'll
// have to be determined at run-time. RVV offers per-data-width operations for
// this so the result doesn't need to be adjusted and that operation is likely
// to fold into the surrounding code for free.
// to fold into the surrounding code for Sleef_free.
//
#define VECTLENSP (__riscv_vsetvlmax_e32m1())
#define VECTLENDP SLEEF_RVV_DP_RUNTIME_VL()
Expand Down Expand Up @@ -273,7 +273,7 @@ typedef vfloat64m2x4_t tdi_t;
// The configuration didn't provide a constant vector length, meaning it'll
// have to be determined at run-time. RVV offers per-data-width operations for
// this so the result doesn't need to be adjusted and that operation is likely
// to fold into the surrounding code for free.
// to fold into the surrounding code for Sleef_free.
//
#define VECTLENSP (__riscv_vsetvlmax_e32m2())
#define VECTLENDP SLEEF_RVV_DP_RUNTIME_VL()
Expand Down
36 changes: 18 additions & 18 deletions src/common/arraymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ArrayMap *initArrayMap() {

for(int i=0;i<NBUCKETS;i++) {
thiz->capacity[i] = 8;
thiz->array[i] = (ArrayMapNode *)malloc(thiz->capacity[i] * sizeof(ArrayMapNode));
thiz->array[i] = (ArrayMapNode *)Sleef_malloc(thiz->capacity[i] * sizeof(ArrayMapNode));
thiz->size[i] = 0;
}

Expand All @@ -101,11 +101,11 @@ void ArrayMap_dispose(ArrayMap *thiz) {
assert(thiz->array[j][i].magic == MAGIC_ARRAYMAPNODE);
thiz->array[j][i].magic = 0;
}
free(thiz->array[j]);
Sleef_free(thiz->array[j]);
}

thiz->magic = 0;
free(thiz);
Sleef_free(thiz);
}

int ArrayMap_size(ArrayMap *thiz) {
Expand All @@ -115,7 +115,7 @@ int ArrayMap_size(ArrayMap *thiz) {

uint64_t *ArrayMap_keyArray(ArrayMap *thiz) {
assert(thiz != NULL && thiz->magic == MAGIC_ARRAYMAP);
uint64_t *a = (uint64_t *)malloc(sizeof(uint64_t) * thiz->totalSize);
uint64_t *a = (uint64_t *)Sleef_malloc(sizeof(uint64_t) * thiz->totalSize);
int p = 0;
for(int j=0;j<NBUCKETS;j++) {
for(int i=0;i<thiz->size[j];i++) {
Expand All @@ -128,7 +128,7 @@ uint64_t *ArrayMap_keyArray(ArrayMap *thiz) {

void **ArrayMap_valueArray(ArrayMap *thiz) {
assert(thiz != NULL && thiz->magic == MAGIC_ARRAYMAP);
void **a = (void **)malloc(sizeof(void *) * thiz->totalSize);
void **a = (void **)Sleef_malloc(sizeof(void *) * thiz->totalSize);
int p = 0;
for(int j=0;j<NBUCKETS;j++) {
for(int i=0;i<thiz->size[j];i++) {
Expand Down Expand Up @@ -218,7 +218,7 @@ ArrayMap *ArrayMap_load(const char *fn, const char *prefix, const char *idstr, i

ArrayMap *thiz = initArrayMap();

char *prefix2 = malloc(prefixLen+10);
char *prefix2 = Sleef_malloc(prefixLen+10);
strcpy(prefix2, prefix);
String_trim(prefix2);
for(char *p = prefix2;*p != '\0';p++) {
Expand All @@ -228,15 +228,15 @@ ArrayMap *ArrayMap_load(const char *fn, const char *prefix, const char *idstr, i
strcat(prefix2, " : ");
prefixLen = (int)strlen(prefix2);

char *line = malloc(sizeof(char) * (LINELEN+10));
char *line = Sleef_malloc(sizeof(char) * (LINELEN+10));
line[idstrlen] = '\0';

if (fread(line, sizeof(char), idstrlen, fp) != idstrlen ||
strcmp(idstr, line) != 0) {
if (doLock) FUNLOCK(fp);
fclose(fp);
free(prefix2);
free(line);
Sleef_free(prefix2);
Sleef_free(line);
return NULL;
}

Expand All @@ -246,20 +246,20 @@ ArrayMap *ArrayMap_load(const char *fn, const char *prefix, const char *idstr, i
if (strncmp(line, prefix2, prefixLen) != 0) continue;

uint64_t key;
char *value = malloc(sizeof(char) * LINELEN);
char *value = Sleef_malloc(sizeof(char) * LINELEN);

if (sscanf(line + prefixLen, "%" SCNx64 " : %s\n", &key, value) == 2) {
ArrayMap_put(thiz, (uint64_t)key, (void *)value);
} else {
free(value);
Sleef_free(value);
}
}

if (doLock) FUNLOCK(fp);
fclose(fp);

free(prefix2);
free(line);
Sleef_free(prefix2);
Sleef_free(line);

return thiz;
}
Expand All @@ -274,7 +274,7 @@ int ArrayMap_save(ArrayMap *thiz, const char *fn, const char *prefix, const char

// Generate prefix2

char *prefix2 = malloc(prefixLen+10);
char *prefix2 = Sleef_malloc(prefixLen+10);
strcpy(prefix2, prefix);
String_trim(prefix2);
for(char *p = prefix2;*p != '\0';p++) {
Expand All @@ -301,7 +301,7 @@ int ArrayMap_save(ArrayMap *thiz, const char *fn, const char *prefix, const char
return -1;
}

char *line = malloc(sizeof(char) * (LINELEN+10));
char *line = Sleef_malloc(sizeof(char) * (LINELEN+10));
line[idstrlen] = '\0';

if (fread(line, sizeof(char), idstrlen, fp) == idstrlen && strcmp(idstr, line) == 0) {
Expand All @@ -323,7 +323,7 @@ int ArrayMap_save(ArrayMap *thiz, const char *fn, const char *prefix, const char
fprintf(tmpfp, "%s %" PRIx64 " : %s\n", prefix2, keys[i], value);
}

free(keys);
Sleef_free(keys);

fseek(fp, 0, SEEK_SET);
FTRUNCATE(fp, 0);
Expand All @@ -341,7 +341,7 @@ int ArrayMap_save(ArrayMap *thiz, const char *fn, const char *prefix, const char
fclose(fp);

CLOSETMPFILE(tmpfp);
free(prefix2);
free(line);
Sleef_free(prefix2);
Sleef_free(line);
return 0;
}
72 changes: 69 additions & 3 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <assert.h>

#include "misc.h"

#if 0
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
#include <sys/timeb.h>

Expand All @@ -25,7 +25,7 @@ EXPORT uint64_t Sleef_currentTimeMicros() {
#include <sys/time.h>

EXPORT void *Sleef_malloc(size_t z) { void *ptr = NULL; posix_memalign(&ptr, 256, z); return ptr; }
EXPORT void Sleef_free(void *ptr) { free(ptr); }
EXPORT void Sleef_free(void *ptr) { Sleef_free(ptr); }

EXPORT uint64_t Sleef_currentTimeMicros() {
struct timeval time;
Expand All @@ -42,14 +42,80 @@ EXPORT uint64_t Sleef_currentTimeMicros() {
#endif

EXPORT void *Sleef_malloc(size_t z) { void *ptr = NULL; posix_memalign(&ptr, 4096, z); return ptr; }
EXPORT void Sleef_free(void *ptr) { free(ptr); }
EXPORT void Sleef_free(void *ptr) { Sleef_free(ptr); }

EXPORT uint64_t Sleef_currentTimeMicros() {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return (uint64_t)tp.tv_sec * INT64_C(1000000) + ((uint64_t)tp.tv_nsec/1000);
}
#endif // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
#else

#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
#include <sys/timeb.h>
#define MEM_ALIGN_SIZE 256
#elif defined(__APPLE__)
#include <sys/time.h>
#define MEM_ALIGN_SIZE 256
#else // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
#include <time.h>
#include <unistd.h>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif

#define MEM_ALIGN_SIZE 4096

#endif

EXPORT void *Sleef_malloc(size_t z)
{
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
return _aligned_malloc(z, MEM_ALIGN_SIZE);
#elif defined(__APPLE__)
void *ptr = NULL; posix_memalign(&ptr, MEM_ALIGN_SIZE, z);
return ptr;
#else // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
void *ptr = NULL;
posix_memalign(&ptr, MEM_ALIGN_SIZE, z);
return ptr;
#endif
}

EXPORT void Sleef_free(void *ptr)
{
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
_aligned_free(ptr);
#elif defined(__APPLE__)
Sleef_free(ptr);
#else // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
Sleef_free(ptr);
#endif
}

EXPORT uint64_t Sleef_currentTimeMicros()
{
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
struct __timeb64 t;
_ftime64(&t);
return t.time * INT64_C(1000000) + t.millitm*1000;
#elif defined(__APPLE__)
struct timeval time;
gettimeofday(&time, NULL);
return (uint64_t)((time.tv_sec * INT64_C(1000000)) + time.tv_usec);
#else // #if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER)
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return (uint64_t)tp.tv_sec * INT64_C(1000000) + ((uint64_t)tp.tv_nsec/1000);
#endif
}

#endif


#ifdef _MSC_VER
#include <intrin.h>
Expand Down
44 changes: 22 additions & 22 deletions src/dft-tester/naivetest.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int check_cf(int n) {
real *sx = (real *)Sleef_malloc(n*2 * sizeof(real));
real *sy = (real *)Sleef_malloc(n*2 * sizeof(real));

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -144,8 +144,8 @@ int check_cf(int n) {

//

free(fs);
free(ts);
Sleef_free(fs);
Sleef_free(ts);

Sleef_free(sx);
Sleef_free(sy);
Expand All @@ -163,8 +163,8 @@ int check_cb(int n) {
real *sx = (real *)Sleef_malloc(sizeof(real)*n*2);
real *sy = (real *)Sleef_malloc(sizeof(real)*n*2);

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -198,8 +198,8 @@ int check_cb(int n) {

//

free(fs);
free(ts);
Sleef_free(fs);
Sleef_free(ts);

Sleef_free(sx);
Sleef_free(sy);
Expand All @@ -217,8 +217,8 @@ int check_rf(int n) {
real *sx = (real *)Sleef_malloc(n * sizeof(real));
real *sy = (real *)Sleef_malloc((n/2+1)*sizeof(real)*2);

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -251,8 +251,8 @@ int check_rf(int n) {

//

free(fs);
free(ts);
Sleef_free(fs);
Sleef_free(ts);

Sleef_free(sx);
Sleef_free(sy);
Expand All @@ -267,8 +267,8 @@ int check_rf(int n) {
int check_rb(int n) {
int i;

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -319,8 +319,8 @@ int check_rb(int n) {

//

free(fs);
free(ts);
Sleef_free(fs);
Sleef_free(ts);

Sleef_free(sx);
Sleef_free(sy);
Expand All @@ -337,8 +337,8 @@ int check_arf(int n) {
real *sx = (real *)Sleef_malloc(n * sizeof(real));
real *sy = (real *)Sleef_malloc(n * sizeof(real));

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -391,8 +391,8 @@ int check_arb(int n) {
real *sx = (real *)Sleef_malloc(n * sizeof(real));
real *sy = (real *)Sleef_malloc(n * sizeof(real));

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

//

Expand Down Expand Up @@ -445,8 +445,8 @@ int check_arb(int n) {

//

free(fs);
free(ts);
Sleef_free(fs);
Sleef_free(ts);

Sleef_free(sx);
Sleef_free(sy);
Expand Down
6 changes: 3 additions & 3 deletions src/dft-tester/tutorial.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ int main(int argc, char **argv) {
exit(-1);
}

cmpl *ts = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)malloc(sizeof(cmpl)*n);
cmpl *ts = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);
cmpl *fs = (cmpl *)Sleef_malloc(sizeof(cmpl)*n);

for(int i=0;i<n;i++) {
ts[i] =
Expand All @@ -71,7 +71,7 @@ int main(int argc, char **argv) {

printf("%s\n", success ? "OK" : "NG");

free(fs); free(ts);
Sleef_free(fs); Sleef_free(ts);
Sleef_free(sy); Sleef_free(sx);

SleefDFT_dispose(p);
Expand Down
Loading
Loading