Skip to content

Commit

Permalink
mem
Browse files Browse the repository at this point in the history
  • Loading branch information
i-evi committed Dec 11, 2020
1 parent 0f464e1 commit 885b254
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/cc_tensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>

#include "cc_macro.h"
#include "cc_assert.h"
#include "cc_tensor.h"
#include "cc_tsrmgr.h"
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
23 changes: 22 additions & 1 deletion src/cc_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif

#include "util_list.h"
#include "cc_macro.h"
#include "cc_dtype.h"

enum cc_tensor_items {
Expand All @@ -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(
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cc_tsrmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "util_rbt.h"
#include "util_log.h"
#include "cc_macro.h"
#include "cc_assert.h"
#include "cc_tsrmgr.h"

Expand Down Expand Up @@ -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*)
Expand Down
5 changes: 2 additions & 3 deletions src/cc_tsrmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
Expand Down

0 comments on commit 885b254

Please sign in to comment.