commit f0a8f0da43bd752685471c95f6176c0dba93a74b
parent 6abeb5f1b03f8db60bf58bd213c91ca4a7a44a25
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 15 Jan 2021 13:44:20 +0100
Add the dump_memory_size internal function
Diffstat:
2 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/src/atrstm.c b/src/atrstm.c
@@ -13,6 +13,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#define _POSIX_C_SOURCE 200112L /* snprintf support */
+
#include "atrstm.h"
#include "atrstm_build_octrees.h"
#include "atrstm_c.h"
@@ -198,3 +200,55 @@ atrstm_ref_put(struct atrstm* atrstm)
ref_put(&atrstm->ref, release_atrstm);
return RES_OK;
}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+void
+dump_memory_size
+ (const size_t size, /* In Bytes */
+ size_t* real_dump_len, /* May be NULL */
+ char* dump, /* May be NULL */
+ size_t max_dump_len)
+{
+ size_t available_dump_space = max_dump_len;
+ char* dst = dump;
+ const size_t KILO_BYTE = 1024;
+ const size_t MEGA_BYTE = 1024*KILO_BYTE;
+ const size_t GIGA_BYTE = 1024*MEGA_BYTE;
+ size_t ngigas, nmegas, nkilos;
+ size_t nbytes = size;
+
+ #define DUMP(Size, Suffix) \
+ { \
+ const int len = snprintf \
+ (dst, available_dump_space, \
+ "%li %s", (long)Size, Size > 1 ? Suffix "s ": Suffix " "); \
+ ASSERT(len >= 0); \
+ if(real_dump_len) { \
+ real_dump_len += len; \
+ } \
+ if((size_t)len < available_dump_space) { \
+ dst += len; \
+ available_dump_space -= (size_t)len; \
+ } else if(dst) { \
+ available_dump_space = 0; \
+ dst = NULL; \
+ } \
+ } (void) 0
+
+ if((ngigas = nbytes / GIGA_BYTE) != 0) {
+ DUMP(ngigas, "GB");
+ nbytes -= ngigas * GIGA_BYTE;
+ }
+ if((nmegas = nbytes / MEGA_BYTE) != 0) {
+ DUMP(nmegas, "MB");
+ nbytes -= nmegas * MEGA_BYTE;
+ }
+ if((nkilos = nbytes / KILO_BYTE) != 0) {
+ DUMP(nkilos, "kB");
+ nbytes -= nkilos * KILO_BYTE;
+ }
+ DUMP(nbytes, "Byte");
+ #undef DUMP
+}
diff --git a/src/atrstm_c.h b/src/atrstm_c.h
@@ -63,4 +63,11 @@ setup_unstructured_volumetric_mesh
const char* sth_filename,
struct suvm_volume** out_volume);
+extern LOCAL_SYM void
+dump_memory_size
+ (const size_t size, /* In Bytes */
+ size_t* real_dump_len, /* May be NULL */
+ char* dump, /* May be NULL */
+ size_t max_dump_len);
+
#endif /* ATRSTM_C_H */