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

Move memory related functions to memory.[ch]pp #2677

Merged
merged 6 commits into from
Jan 19, 2024
Merged
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
2 changes: 2 additions & 0 deletions cmake/NeuronFileLists.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(HEADER_FILES_TO_INSTALL
oc/hocparse.h
oc/mcran4.h
oc/mech_api.h
oc/memory.hpp
oc/nrnapi.h
oc/nrnassrt.h
oc/nrnisaac.h
Expand Down Expand Up @@ -101,6 +102,7 @@ set(OC_FILE_LIST
hoc_oop.cpp
list.cpp
math.cpp
memory.cpp
mswinprt.cpp
nonlin.cpp
ocerf.cpp
Expand Down
4 changes: 0 additions & 4 deletions src/oc/hocdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ struct HocParmUnits { /* units for symbol values */

#include "oc_ansi.h"

void* emalloc(size_t n);
void* ecalloc(size_t n, size_t size);
void* erealloc(void* ptr, size_t n);

extern Inst *hoc_progp, *hoc_progbase, *hoc_prog, *hoc_prog_parse_recover;
extern Inst* hoc_pc;

Expand Down
112 changes: 112 additions & 0 deletions src/oc/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "memory.hpp"

#include <cstdlib>
#include <cstring>

// For hoc_warning and hoc_execerror
#include "oc_ansi.h"

#if HAVE_POSIX_MEMALIGN
#define HAVE_MEMALIGN 1
#endif
#if defined(DARWIN) /* posix_memalign seems not to work on Darwin 10.6.2 */
#undef HAVE_MEMALIGN
#endif
#if HAVE_MEMALIGN
#undef _XOPEN_SOURCE /* avoid warnings about redefining this */
#define _XOPEN_SOURCE 600
#endif

static bool emalloc_error = false;

void* hoc_Emalloc(std::size_t n) { /* check return from malloc */
void* p = std::malloc(n);
if (p == nullptr) {
emalloc_error = true;

Check warning on line 25 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L25

Added line #L25 was not covered by tests
}
return p;
}

void* hoc_Ecalloc(std::size_t n, std::size_t size) { /* check return from calloc */
if (n == 0) {
return nullptr;
}
void* p = std::calloc(n, size);
if (p == nullptr) {
emalloc_error = true;

Check warning on line 36 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L36

Added line #L36 was not covered by tests
}
return p;
}

void* hoc_Erealloc(void* ptr, std::size_t size) { /* check return from realloc */
if (!ptr) {
return hoc_Emalloc(size);
}
void* p = std::realloc(ptr, size);
if (p == nullptr) {
std::free(ptr);
emalloc_error = true;

Check warning on line 48 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L47-L48

Added lines #L47 - L48 were not covered by tests
}
return p;
}

void hoc_malchk(void) {
if (emalloc_error) {
emalloc_error = false;
hoc_execerror("out of memory", nullptr);

Check warning on line 56 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L55-L56

Added lines #L55 - L56 were not covered by tests
}
}

void* emalloc(std::size_t n) {
void* p = hoc_Emalloc(n);
if (emalloc_error) {
hoc_malchk();

Check warning on line 63 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L63

Added line #L63 was not covered by tests
}
return p;
}

void* ecalloc(std::size_t n, std::size_t size) {
void* p = hoc_Ecalloc(n, size);
if (emalloc_error) {
hoc_malchk();

Check warning on line 71 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L71

Added line #L71 was not covered by tests
}
return p;
}

void* erealloc(void* ptr, std::size_t size) {
void* p = hoc_Erealloc(ptr, size);
if (emalloc_error) {
hoc_malchk();

Check warning on line 79 in src/oc/memory.cpp

View check run for this annotation

Codecov / codecov/patch

src/oc/memory.cpp#L79

Added line #L79 was not covered by tests
}
return p;
}

void* nrn_cacheline_alloc(void** memptr, std::size_t size) {
#if HAVE_MEMALIGN
static bool memalign_is_working = true;
if (memalign_is_working) {
if (posix_memalign(memptr, 64, size) != 0) {
hoc_warning("posix_memalign not working, falling back to using malloc\n", nullptr);
memalign_is_working = false;
*memptr = hoc_Emalloc(size);
hoc_malchk();
}
} else
#endif
{
*memptr = hoc_Emalloc(size);
hoc_malchk();
}
return *memptr;
}

void* nrn_cacheline_calloc(void** memptr, std::size_t nmemb, std::size_t size) {
#if HAVE_MEMALIGN
nrn_cacheline_alloc(memptr, nmemb * size);
std::memset(*memptr, 0, nmemb * size);
#else
*memptr = hoc_Ecalloc(nmemb, size);
hoc_malchk();
#endif
return *memptr;
}
18 changes: 18 additions & 0 deletions src/oc/memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

// Some functions here are prepend with 'hoc_' but they are unrelated to hoc itself.
#include <cstddef>

/* check return from malloc */
void* hoc_Emalloc(std::size_t n);
void* hoc_Ecalloc(std::size_t n, std::size_t size);
void* hoc_Erealloc(void* ptr, std::size_t size);

void hoc_malchk(void);

void* emalloc(std::size_t n);
void* ecalloc(std::size_t n, std::size_t size);
void* erealloc(void* ptr, std::size_t size);

void* nrn_cacheline_alloc(void** memptr, std::size_t size);
void* nrn_cacheline_calloc(void** memptr, std::size_t nmemb, std::size_t size);
9 changes: 2 additions & 7 deletions src/oc/oc_ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdexcept>
#include <string>
#include <string_view>

#include "memory.hpp"
/**
* \dir
* \brief HOC Interpreter
Expand Down Expand Up @@ -49,9 +51,6 @@ void ivoc_help(const char*);

Symbol* hoc_lookup(const char*);

void* hoc_Ecalloc(std::size_t nmemb, std::size_t size);
void* hoc_Emalloc(size_t size);
void hoc_malchk();
[[noreturn]] void hoc_execerror(const char*, const char*);
[[noreturn]] void hoc_execerr_ext(const char* fmt, ...);
char* hoc_object_name(Object*);
Expand Down Expand Up @@ -320,10 +319,6 @@ void hoc_obj_set(int i, Object*);
void nrn_hoc_lock();
void nrn_hoc_unlock();

void* hoc_Erealloc(void* ptr, std::size_t size);

void* nrn_cacheline_alloc(void** memptr, std::size_t size);
void* nrn_cacheline_calloc(void** memptr, std::size_t nmemb, std::size_t size);
[[noreturn]] void nrn_exit(int);
void hoc_free_list(Symlist**);
int hoc_errno_check();
Expand Down
101 changes: 0 additions & 101 deletions src/oc/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@
/* /local/src/master/nrn/src/oc/symbol.cpp,v 1.9 1999/02/25 18:01:58 hines Exp */
/* version 7.2.1 2-jan-89 */

#if HAVE_POSIX_MEMALIGN
#define HAVE_MEMALIGN 1
#endif
#if defined(DARWIN) /* posix_memalign seems not to work on Darwin 10.6.2 */
#undef HAVE_MEMALIGN
#endif
#if HAVE_MEMALIGN
#undef _XOPEN_SOURCE /* avoid warnings about redefining this */
#define _XOPEN_SOURCE 600
#endif

#include "hoc.h"
#include "hocdec.h"
#include "hoclist.h"
Expand Down Expand Up @@ -173,96 +162,6 @@ void hoc_link_symbol(Symbol* sp, Symlist* list) {
sp->next = nullptr;
}

static int emalloc_error = 0;

void hoc_malchk(void) {
if (emalloc_error) {
emalloc_error = 0;
execerror("out of memory", nullptr);
}
}

void* hoc_Emalloc(size_t n) { /* check return from malloc */
void* p = malloc(n);
if (p == nullptr)
emalloc_error = 1;
return p;
}

void* emalloc(size_t n) {
void* p = hoc_Emalloc(n);
if (emalloc_error) {
hoc_malchk();
}
return p;
}

void* hoc_Ecalloc(size_t n, size_t size) { /* check return from calloc */
if (n == 0) {
return nullptr;
}
void* p = calloc(n, size);
if (p == nullptr)
emalloc_error = 1;
return p;
}

void* ecalloc(size_t n, size_t size) {
void* p = hoc_Ecalloc(n, size);
if (emalloc_error) {
hoc_malchk();
}
return p;
}

void* nrn_cacheline_alloc(void** memptr, size_t size) {
#if HAVE_MEMALIGN
static int memalign_is_working = 1;
if (memalign_is_working) {
if (posix_memalign(memptr, 64, size) != 0) {
fprintf(stderr, "posix_memalign not working, falling back to using malloc\n");
memalign_is_working = 0;
*memptr = hoc_Emalloc(size);
hoc_malchk();
}
} else
#endif
*memptr = hoc_Emalloc(size);
hoc_malchk();
return *memptr;
}

void* nrn_cacheline_calloc(void** memptr, size_t nmemb, size_t size) {
#if HAVE_MEMALIGN
nrn_cacheline_alloc(memptr, nmemb * size);
memset(*memptr, 0, nmemb * size);
#else
*memptr = hoc_Ecalloc(nmemb, size);
hoc_malchk();
#endif
return *memptr;
}

void* hoc_Erealloc(void* ptr, size_t size) { /* check return from realloc */
if (!ptr) {
return hoc_Emalloc(size);
}
void* p = realloc(ptr, size);
if (p == nullptr) {
free(ptr);
emalloc_error = 1;
}
return p;
}

void* erealloc(void* ptr, size_t size) {
void* p = hoc_Erealloc(ptr, size);
if (emalloc_error) {
hoc_malchk();
}
return p;
}

void hoc_free_symspace(Symbol* s1) { /* frees symbol space. Marks it UNDEF */
if (s1 && s1->cpublic != 2) {
switch (s1->type) {
Expand Down
Loading