1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-06-30 00:44:52 +02:00
qmckl/src/qmckl_memory.org

96 lines
2.1 KiB
Org Mode
Raw Normal View History

2020-10-16 23:56:22 +02:00
# -*- mode: org -*-
# vim: syntax=c
#+TITLE: Memory management
We override the allocation functions to enable the possibility of
optimized libraries to fine-tune the memory allocation.
3 files are produced:
- a header file : =qmckl_memory.h=
- a source file : =qmckl_memory.c=
- a test file : =test_qmckl_memory.c=
*** Header
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
2020-10-16 23:56:22 +02:00
#ifndef QMCKL_MEMORY_H
#define QMCKL_MEMORY_H
#include "qmckl.h"
#+END_SRC
*** Source
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
2020-10-16 23:56:22 +02:00
#include <stdlib.h>
#include "qmckl_memory.h"
#+END_SRC
*** Test
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
2020-10-17 01:10:54 +02:00
#include "qmckl.h"
#include "munit.h"
2020-10-21 19:50:18 +02:00
MunitResult test_qmckl_memory() {
2020-10-16 23:56:22 +02:00
#+END_SRC
** =qmckl_malloc=
2020-10-22 00:50:07 +02:00
Analogous of =malloc, but passing a context and a signed 64-bit integers as argument.=
2020-10-16 23:56:22 +02:00
*** Header
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
2020-10-22 00:50:07 +02:00
void* qmckl_malloc(const qmckl_context ctx, const size_t size);
2020-10-16 23:56:22 +02:00
#+END_SRC
*** Source
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
2020-10-22 00:50:07 +02:00
void* qmckl_malloc(const qmckl_context ctx, const size_t size) {
if (ctx == (qmckl_context) 0) {
/* Avoids unused parameter error */
return malloc( (size_t) size );
}
2020-10-16 23:56:22 +02:00
return malloc( (size_t) size );
}
#+END_SRC
*** Test
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
2020-10-16 23:56:22 +02:00
int *a;
2020-10-22 00:50:07 +02:00
a = (int*) qmckl_malloc( (qmckl_context) 1, 3*sizeof(int));
2020-10-16 23:56:22 +02:00
a[0] = 1;
a[1] = 2;
a[2] = 3;
2020-10-17 01:10:54 +02:00
munit_assert_int(a[0], ==, 1);
munit_assert_int(a[1], ==, 2);
munit_assert_int(a[2], ==, 3);
2020-10-16 23:56:22 +02:00
#+END_SRC
** =qmckl_free=
*** Header
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
2020-10-16 23:56:22 +02:00
void qmckl_free(void *ptr);
#+END_SRC
*** Source
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
2020-10-16 23:56:22 +02:00
void qmckl_free(void *ptr) {
free(ptr);
}
#+END_SRC
*** Test
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
2020-10-16 23:56:22 +02:00
qmckl_free(a);
#+END_SRC
* End of files
*** Header
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
2020-10-16 23:56:22 +02:00
#endif
#+END_SRC
*** Test
2020-10-21 19:50:18 +02:00
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
2020-10-17 01:10:54 +02:00
return MUNIT_OK;
}
2020-10-16 23:56:22 +02:00
#+END_SRC