From 885b25429e0e5c683c1790535192e286d8f86cf2 Mon Sep 17 00:00:00 2001 From: i-evi Date: Fri, 11 Dec 2020 21:52:28 +0800 Subject: [PATCH] mem --- src/cc_tensor.c | 26 ++++++++++++++++++++------ src/cc_tensor.h | 23 ++++++++++++++++++++++- src/cc_tsrmgr.c | 4 ++-- src/cc_tsrmgr.h | 5 ++--- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/cc_tensor.c b/src/cc_tensor.c index 2299006..90afd2d 100644 --- a/src/cc_tensor.c +++ b/src/cc_tensor.c @@ -2,6 +2,7 @@ #include #include +#include "cc_macro.h" #include "cc_assert.h" #include "cc_tensor.h" #include "cc_tsrmgr.h" @@ -17,8 +18,7 @@ cc_tensor_t *cc_create(const cc_ssize *shape, memsize = cc_dtype_size(dtype) * elems; if (!memsize) return NULL; - cc_assert_alloc( - tensor = (cc_tensor_t*)malloc(sizeof(cc_tensor_t))); + cc_assert_alloc(tensor = CC_ALLOC(cc_tensor_t)); cc_assert_ptr( tensor->container = list_new(CC_TENSOR_ITEMS, 0)); cc_assert_alloc( @@ -51,8 +51,7 @@ cc_tensor_t *cc_create(const cc_ssize *shape, cc_tensor_t *cc_copy(const cc_tensor_t *tensor, const char *name) { cc_tensor_t *copied; - cc_assert_alloc( - copied = (cc_tensor_t*)malloc(sizeof(cc_tensor_t))); + cc_assert_alloc(copied = CC_ALLOC(cc_tensor_t)); cc_assert_ptr( copied->container = list_clone(tensor->container)); cc_assert_ptr( @@ -77,8 +76,7 @@ cc_tensor_t *cc_copy(const cc_tensor_t *tensor, const char *name) cc_tensor_t *cc_load(const char *filename) { cc_tensor_t *tensor; - cc_assert_alloc( - tensor = (cc_tensor_t*)malloc(sizeof(cc_tensor_t))); + cc_assert_alloc(tensor = CC_ALLOC(cc_tensor_t)); if (!(tensor->container = list_import(filename))) { free(tensor); return NULL; @@ -122,6 +120,8 @@ void cc_save(const cc_tensor_t *tensor, const char *filename) void cc_free(cc_tensor_t *tensor) { if (tensor) { + if (tensor->owner) + *tensor->owner = NULL; list_del(tensor->container); free(tensor); } @@ -151,3 +151,17 @@ void cc_property(const cc_tensor_t *tensor) "tensor: \"%s\", dtype: \"%s\", shape: [%s]\n", tensor->name, cc_dtype_to_string(*tensor->dtype), buf); } + +void cc_ptr_bind(cc_tensor_t *tensor, cc_tensor_t **owner) +{ + /* cc_assert_zero(tensor->owner) */ + if (tensor->owner) { + return; + } + tensor->owner = owner; +} + +void cc_ptr_unbind(cc_tensor_t *tensor) +{ + tensor->owner = NULL; +} diff --git a/src/cc_tensor.h b/src/cc_tensor.h index 2829c18..679af44 100644 --- a/src/cc_tensor.h +++ b/src/cc_tensor.h @@ -6,6 +6,7 @@ #endif #include "util_list.h" +#include "cc_macro.h" #include "cc_dtype.h" enum cc_tensor_items { @@ -21,12 +22,14 @@ enum cc_tensor_items { * |_____ name */ -typedef struct { +typedef struct cc_tensor_struct { struct list *container; const char *name; unsigned char *data; const cc_dtype *dtype; const cc_ssize *shape; + cc_uint32 flag; + struct cc_tensor_struct **owner; } cc_tensor_t; cc_tensor_t *cc_create( @@ -45,6 +48,24 @@ void cc_free(cc_tensor_t *tensor); void cc_property(const cc_tensor_t *tensor); +#define CC_FL_FREE 1 +#define CC_FL_CACHED 2 + +CC_INLINE +cc_uint32 cc_getflag(cc_tensor_t *tensor) +{ + return tensor->flag; +} + +CC_INLINE +void cc_setflag(cc_tensor_t *tensor, cc_uint32 flag) +{ + tensor->flag = flag; +} + +void cc_ptr_bind(cc_tensor_t *tensor, cc_tensor_t **owner); +void cc_ptr_unbind(cc_tensor_t *tensor); + #ifdef __cplusplus } #endif diff --git a/src/cc_tsrmgr.c b/src/cc_tsrmgr.c index cef02bb..459a17f 100644 --- a/src/cc_tsrmgr.c +++ b/src/cc_tsrmgr.c @@ -4,6 +4,7 @@ #include "util_rbt.h" #include "util_log.h" +#include "cc_macro.h" #include "cc_assert.h" #include "cc_tsrmgr.h" @@ -291,8 +292,7 @@ void cc_tsrmgr_unpack(struct list *tls) off += (len + sizeof(rlen_t)); } cc_tsrmgr_del(name); - cc_assert_alloc( - t = (cc_tensor_t*)malloc(sizeof(cc_tensor_t))); + cc_assert_alloc(t = CC_ALLOC(cc_tensor_t)); t->container = container; t->name = container->name; t->data = (cc_uint8*) diff --git a/src/cc_tsrmgr.h b/src/cc_tsrmgr.h index 295ff87..91afe62 100644 --- a/src/cc_tsrmgr.h +++ b/src/cc_tsrmgr.h @@ -12,13 +12,12 @@ void cc_tsrmgr_clear(void); void cc_tsrmgr_reg(cc_tensor_t *tensor); void cc_tsrmgr_del(const char *name); -void cc_tsrmgr_update(cc_tensor_t *tensor); void cc_tsrmgr_replace(cc_tensor_t *tensor); -int cc_tsrmgr_status(void); - cc_tensor_t *cc_tsrmgr_get(const char *name); +int cc_tsrmgr_status(void); + void cc_tsrmgr_list(void); /*