Add function for estimating the size of an obitype or of an array of
obitype.
This commit is contained in:
53
src/obitypes.c
Normal file
53
src/obitypes.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* obitypes.c
|
||||||
|
*
|
||||||
|
* Created on: 23 mai 2015
|
||||||
|
* Author: coissac
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <obitypes.h>
|
||||||
|
|
||||||
|
size_t obi_sizeof(OBIType_t type) {
|
||||||
|
size_t size=0
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case OBI_VOID: size = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OBI_INT: size = sizeof(obiint_t);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OBI_FLOAT: size = sizeof(obifloat_t);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OBI_CHAR: size = sizeof(obichar_t);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OBI_IDX: size = sizeof(obiidx_t);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t obi_array_sizeof(OBIType_t type,size_t nbelement) {
|
||||||
|
size_t size;
|
||||||
|
size_t rsize;
|
||||||
|
size_t psize;
|
||||||
|
|
||||||
|
size = obi_sizeof(type) * nbelement;
|
||||||
|
|
||||||
|
psize= getpagesize();
|
||||||
|
rsize= size % psize;
|
||||||
|
|
||||||
|
// Round at a size multiple of pagesize
|
||||||
|
if (rsize)
|
||||||
|
size = (size / psize) * psize + psize;
|
||||||
|
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,3 @@
|
|||||||
/****************************************************************************
|
|
||||||
* OBITypes header file *
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file obitypes.h
|
* @file obitypes.h
|
||||||
* @author Celine Mercier
|
* @author Celine Mercier
|
||||||
@ -13,6 +9,7 @@
|
|||||||
#define OBITYPES_H_
|
#define OBITYPES_H_
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define OBIInt_NA (INT32_MIN)
|
#define OBIInt_NA (INT32_MIN)
|
||||||
#define OBIFloat_NA (NAN)
|
#define OBIFloat_NA (NAN)
|
||||||
@ -35,10 +32,10 @@ typedef enum {
|
|||||||
|
|
||||||
typedef enum OBIType {
|
typedef enum OBIType {
|
||||||
OBI_VOID = 0, /**< data type not specified */
|
OBI_VOID = 0, /**< data type not specified */
|
||||||
OBIInt_t, /**< a signed integer value (C type : int32_t) */
|
OBI_INT, /**< a signed integer value (C type : int32_t) */
|
||||||
OBIFloat_t, /**< a floating value (C type : double) */
|
OBI_FLOAT, /**< a floating value (C type : double) */
|
||||||
OBIChar_t, /**< a character (C type : char) */
|
OBI_CHAR, /**< a character (C type : char) */
|
||||||
OBIIdx_t /**< an index in a data structure (C type : size_t) */
|
OBI_IDX /**< an index in a data structure (C type : size_t) */
|
||||||
} OBIType_t, *OBIType_p;
|
} OBIType_t, *OBIType_p;
|
||||||
|
|
||||||
typedef int32_t obiint_t;
|
typedef int32_t obiint_t;
|
||||||
@ -46,5 +43,34 @@ typedef double obifloat_t;
|
|||||||
typedef char obichar_t;
|
typedef char obichar_t;
|
||||||
typedef size_t obiidx_t;
|
typedef size_t obiidx_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief returns the memory size in bytes of an OBIType
|
||||||
|
*
|
||||||
|
* @param type the OBIType code used as query
|
||||||
|
*
|
||||||
|
* @return the size in bytes of the type
|
||||||
|
* @retval 0 on error (unknown type)
|
||||||
|
*
|
||||||
|
* @since May 2015
|
||||||
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
size_t obi_sizeof(OBIType_t type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief returns the size requested to store an array of OBIType
|
||||||
|
*
|
||||||
|
* The returned size is enouth large to store an array of size nbelement
|
||||||
|
* but rounded at a multiple of the memory page size.
|
||||||
|
*
|
||||||
|
* @param type the OBIType code used as query
|
||||||
|
* @param nbelement the number of element to be stored
|
||||||
|
*
|
||||||
|
* @return the size in bytes of the type
|
||||||
|
* @retval 0 on error (unknown type)
|
||||||
|
*
|
||||||
|
* @since May 2015
|
||||||
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
size_t obi_array_sizeof(OBIType_t type,size_t nbelement);
|
||||||
|
|
||||||
#endif /* OBITYPES_H_ */
|
#endif /* OBITYPES_H_ */
|
||||||
|
Reference in New Issue
Block a user