1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-12-22 12:23:56 +01:00

Added qmckl_malloc

This commit is contained in:
Anthony Scemama 2020-10-16 23:56:22 +02:00
parent ec7df80028
commit 97cfd4d2b5
4 changed files with 102 additions and 6 deletions

View File

@ -182,6 +182,7 @@ rm ${nb}.md
** Documentation ** Documentation
- [[qmckl.org][Main QMCkl header file]] - [[qmckl.org][Main QMCkl header file]]
- [[qmckl_memory.org][Memory management]]
- [[qmckl_context.org][Context]] - [[qmckl_context.org][Context]]
** Acknowledgments ** Acknowledgments

View File

@ -42,6 +42,8 @@ typedef int qmckl_exit_code;
header files. header files.
#+BEGIN_SRC C :tangle qmckl.h #+BEGIN_SRC C :tangle qmckl.h
#include <stdlib.h>
#include "qmckl_memory.h"
#include "qmckl_context.h" #include "qmckl_context.h"
#+END_SRC #+END_SRC

View File

@ -19,13 +19,12 @@ C than in Fortran.
*** Source *** Source
#+BEGIN_SRC C :tangle qmckl_context.c #+BEGIN_SRC C :tangle qmckl_context.c
#include <stdlib.h> /* malloc */ #include "qmckl.h"
#include "qmckl_context.h"
#+END_SRC #+END_SRC
*** Test *** Test
#+BEGIN_SRC C :tangle test_qmckl_context.c #+BEGIN_SRC C :tangle test_qmckl_context.c
#include "qmckl_context.h" #include "qmckl.h"
#include <stdio.h> #include <stdio.h>
int main() { int main() {
qmckl_exit_code rc; /* return code */ qmckl_exit_code rc; /* return code */
@ -112,7 +111,7 @@ qmckl_context qmckl_context_create() {
qmckl_context_struct* context; qmckl_context_struct* context;
context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct)); context = (qmckl_context_struct*) qmckl_malloc (sizeof(qmckl_context_struct));
if (context == NULL) { if (context == NULL) {
return (qmckl_context) 0; return (qmckl_context) 0;
} }
@ -166,7 +165,7 @@ qmckl_context qmckl_context_copy(const qmckl_context context) {
return (qmckl_context) 0; return (qmckl_context) 0;
} }
new_context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct)); new_context = (qmckl_context_struct*) qmckl_malloc (sizeof(qmckl_context_struct));
if (new_context == NULL) { if (new_context == NULL) {
return (qmckl_context) 0; return (qmckl_context) 0;
} }
@ -277,7 +276,7 @@ qmckl_exit_code qmckl_context_destroy(qmckl_context context) {
if (ctx == NULL) return QMCKL_FAILURE; if (ctx == NULL) return QMCKL_FAILURE;
ctx->tag = INVALID_TAG; ctx->tag = INVALID_TAG;
free(ctx); qmckl_free(ctx);
return QMCKL_SUCCESS; return QMCKL_SUCCESS;
} }
#+END_SRC #+END_SRC

94
src/qmckl_memory.org Normal file
View File

@ -0,0 +1,94 @@
# -*- 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
#include "qmckl_memory.h"
#include <stdio.h>
int main() {
qmckl_exit_code rc; /* return code */
rc = QMCKL_SUCCESS;
#+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;
if ( a[0] != 1 || a[1] != 2 || a[2] != 3 ) {
fprintf(stderr,"qmckl_malloc: Invalid data\n");
rc = QMCKL_FAILURE;
}
#+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
return QMCKL_SUCCESS;
}
#+END_SRC