ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
mm.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <log/log.h>
14 
15 #include <arch/mem.h>
16 #include <core/core.h>
17 
18 #include <stddef.h>
19 #include <stdlib.h>
20 
30 static inline void *mm_aligned_alloc(size_t alignment, size_t mem_size)
31 {
32  void *ret = mem_aligned_alloc(alignment, mem_size);
33 
34  if(unlikely(mem_size && ret == NULL)) {
35  logger(LOG_FATAL, "%s", "Out of memory!");
36  abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before.
37  }
38  return ret;
39 }
40 
48 static inline void mm_aligned_free(void *ptr)
49 {
50  mem_aligned_free(ptr);
51 }
52 
53 
62 static inline void *mm_alloc(size_t mem_size)
63 {
64  void *ret = malloc(mem_size);
65 
66  if(__builtin_expect(mem_size && !ret, 0)) {
67  logger(LOG_FATAL, "Out of memory!");
68  abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before.
69  }
70  return ret;
71 }
72 
82 static inline void *mm_realloc(void *ptr, size_t mem_size)
83 {
84  void *ret = realloc(ptr, mem_size);
85 
86  if(__builtin_expect(mem_size && !ret, 0)) {
87  logger(LOG_FATAL, "Out of memory!");
88  abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before.
89  }
90  return ret;
91 }
92 
97 static inline void mm_free(void *ptr)
98 {
99  free(ptr);
100 }
@ LOG_FATAL
The logging level reserved to unexpected, fatal conditions.
Definition: ROOT-Sim.h:113
Core ROOT-Sim functionalities.
#define unlikely(exp)
Optimize the branch as likely not taken.
Definition: core.h:51
Logging library.
#define logger(level,...)
Produce a log message.
Definition: log.h:22
Platform specific memory utilities.
#define mem_aligned_free(mem)
An OS-dependent function to free aligned memory.
Definition: mem.h:27
#define mem_aligned_alloc(align, mem_size)
An OS-dependent function to allocate aligned memory.
Definition: mem.h:25
static void * mm_alloc(size_t mem_size)
A version of the stdlib malloc() used internally.
Definition: mm.h:62
static void * mm_aligned_alloc(size_t alignment, size_t mem_size)
A version of the stdlib aligned_alloc() used internally.
Definition: mm.h:30
static void * mm_realloc(void *ptr, size_t mem_size)
A version of the stdlib realloc() used internally.
Definition: mm.h:82
static void mm_free(void *ptr)
A version of the stdlib free() used internally.
Definition: mm.h:97
static void mm_aligned_free(void *ptr)
A version of the stdlib free() for aligned blocks used internally.
Definition: mm.h:48