1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-07-18 17:03:43 +02:00
qmckl/src/qmckl_memory.org

92 lines
1.9 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=
Analogous of =malloc, but passing signed 64-bit integers as argument.=
*** 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_malloc(long long int size);
#+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_malloc(long long int size) {
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;
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
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