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

106 lines
2.2 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
#+BEGIN_SRC C :tangle qmckl_memory.h
#ifndef QMCKL_MEMORY_H
#define QMCKL_MEMORY_H
#include "qmckl.h"
#+END_SRC
*** Source
#+BEGIN_SRC C :tangle qmckl_memory.c
#include <stdlib.h>
#include "qmckl_memory.h"
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_memory.c
2020-10-17 01:10:54 +02:00
#include "qmckl.h"
#include "munit.h"
static MunitResult test_qmckl_memory() {
2020-10-16 23:56:22 +02:00
#+END_SRC
** =qmckl_malloc=
Analogous of =malloc, but passing signed 64-bit integers as argument.=
*** Header
#+BEGIN_SRC C :tangle qmckl_memory.h
void* qmckl_malloc(long long int size);
#+END_SRC
*** Source
#+BEGIN_SRC C :tangle qmckl_memory.c
void* qmckl_malloc(long long int size) {
return malloc( (size_t) size );
}
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_memory.c
int *a;
a = (int*) qmckl_malloc(3*sizeof(int));
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
#+BEGIN_SRC C :tangle qmckl_memory.h
void qmckl_free(void *ptr);
#+END_SRC
*** Source
#+BEGIN_SRC C :tangle qmckl_memory.c
void qmckl_free(void *ptr) {
free(ptr);
}
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_memory.c
qmckl_free(a);
#+END_SRC
* End of files
*** Header
#+BEGIN_SRC C :tangle qmckl_memory.h
#endif
#+END_SRC
*** Test
#+BEGIN_SRC C :tangle test_qmckl_memory.c
2020-10-17 01:10:54 +02:00
return MUNIT_OK;
}
int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
static MunitTest test_suite_tests[] =
{
{ (char*) "qmckl_memory", test_qmckl_memory, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
static const MunitSuite test_suite =
{
(char*) "", test_suite_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
};
return munit_suite_main(&test_suite, (void*) "µnit", argc, argv);
2020-10-16 23:56:22 +02:00
}
#+END_SRC