From 1d5bbb7cfac47124f8f670fe2317e0da89de76c0 Mon Sep 17 00:00:00 2001 From: Ed Hartnett Date: Thu, 1 Jul 2010 13:17:54 +0000 Subject: [PATCH] reduced memory use of type structs for netcdf-4 --- libsrc4/nc4file.c | 4 ++ libsrc4/nc4internal.c | 4 ++ libsrc4/nc4internal.h | 2 +- libsrc4/nc4type.c | 2 + libsrc4/tst_lists.c | 131 ------------------------------------------ 5 files changed, 11 insertions(+), 132 deletions(-) delete mode 100644 libsrc4/tst_lists.c diff --git a/libsrc4/nc4file.c b/libsrc4/nc4file.c index 2c3b5d010..60e64d5be 100755 --- a/libsrc4/nc4file.c +++ b/libsrc4/nc4file.c @@ -774,6 +774,8 @@ get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid, *xtype = nc_type_constant[t]; (*type_info)->nc_typeid = nc_type_constant[t]; (*type_info)->size = type_size[t]; + if (!((*type_info)->name = malloc((strlen(type_name[t]) + 1) * sizeof(char)))) + return NC_ENOMEM; strcpy((*type_info)->name, type_name[t]); (*type_info)->class = class; (*type_info)->hdf_typeid = hdf_typeid; @@ -1100,6 +1102,8 @@ read_type(NC_GRP_INFO_T *grp, char *type_name) /* Remember info about this type. */ type->nc_typeid = grp->file->nc4_info->next_typeid++; type->size = type_size; + if (!(type->name = malloc((strlen(type_name) + 1) * sizeof(char)))) + return NC_ENOMEM; strcpy(type->name, type_name); type->class = ud_type_type; type->base_nc_type = base_nc_type; diff --git a/libsrc4/nc4internal.c b/libsrc4/nc4internal.c index 40136a995..a46ba4eeb 100644 --- a/libsrc4/nc4internal.c +++ b/libsrc4/nc4internal.c @@ -1003,6 +1003,10 @@ type_list_del(NC_TYPE_INFO_T **list, NC_TYPE_INFO_T *type) return NC_EHDFERR; } + /* Free the name. */ + if (type->name) + free(type->name); + /* Delete all the fields in this type (there will be some if its a * compound). */ field = type->field; diff --git a/libsrc4/nc4internal.h b/libsrc4/nc4internal.h index e0e8304f2..546e4a089 100644 --- a/libsrc4/nc4internal.h +++ b/libsrc4/nc4internal.h @@ -227,7 +227,7 @@ typedef struct NC_TYPE_INFO hid_t native_typeid; size_t size; int committed; /* What the pig is, but the hen isn't, at breakfast. */ - char name[NC_MAX_NAME + 1]; + char *name; int class; /* NC_VLEN, NC_COMPOUND, NC_OPAQUE, or NC_ENUM */ int num_enum_members; NC_ENUM_MEMBER_INFO_T *enum_member; diff --git a/libsrc4/nc4type.c b/libsrc4/nc4type.c index 91b26e489..d57a5aa4a 100644 --- a/libsrc4/nc4type.c +++ b/libsrc4/nc4type.c @@ -230,6 +230,8 @@ add_user_type(int ncid, size_t size, const char *name, nc_type base_typeid, /* Remember info about this type. */ type->nc_typeid = grp->file->nc4_info->next_typeid++; type->size = size; + if (!(type->name = malloc((strlen(norm_name) + 1) * sizeof(char)))) + return NC_ENOMEM; strcpy(type->name, norm_name); type->class = type_class; type->base_nc_type = base_typeid; diff --git a/libsrc4/tst_lists.c b/libsrc4/tst_lists.c deleted file mode 100644 index ffb651c03..000000000 --- a/libsrc4/tst_lists.c +++ /dev/null @@ -1,131 +0,0 @@ -/* This is part of the netCDF package. - Copyright 2005 University Corporation for Atmospheric Research/Unidata - See COPYRIGHT file for conditions of use. - - Test internal netcdf-4 list code. - $Id: tst_lists.c,v 1.20 2009/09/14 19:42:51 ed Exp $ -*/ - -#include "netcdf.h" -#include <../libsrc/nc.h> -#include "nc4internal.h" -#include - -extern NC_FILE_INFO_T *nc_file; - -#define NAME "lalala" - -int -main(int argc, char **argv) -{ - printf("\n*** Testing netcdf-4 internal list code.\n"); - printf("*** testing group list code..."); - { - NC_GRP_INFO_T *g = NULL, *g1; - NC_FILE_INFO_T *nc = NULL; - - /*nc_set_log_level(6);*/ - - /* Add a file, then delete it. */ - if (nc4_file_list_add(0x00010000)) ERR; - if (!(nc = nc4_find_nc_file(0x00010000))) ERR; - nc4_file_list_del(nc); - - /* Add netcdf-4 structs, including the root group, then delete - * them all. */ - if (nc4_file_list_add(0x00010000)) ERR; - if (!(nc = nc4_find_nc_file(0x00010000))) ERR; - if (nc4_nc4f_list_add(nc, NAME, NC_NETCDF4)) ERR; - if (!nc->nc4_info) ERR; - if (strcmp(NAME, nc->nc4_info->path)) ERR; - nc4_rec_grp_del(&nc->nc4_info->root_grp, nc->nc4_info->root_grp); - free(nc->nc4_info); - nc4_file_list_del(nc); - - /* Add a child group to the root group. */ - if (nc4_file_list_add(0x00010000)) ERR; - if (!(nc = nc4_find_nc_file(0x00010000))) ERR; - if (nc4_nc4f_list_add(nc, NAME, NC_NETCDF4)) ERR; - if (nc4_grp_list_add(&nc->nc4_info->root_grp->children, 1, nc->nc4_info->root_grp, - nc, "hohoho", &g1)) ERR; - if (!(g = nc4_rec_find_grp(nc->nc4_info->root_grp, 1))) ERR; - if (g->nc_grpid != 1) ERR; - nc4_rec_grp_del(&nc->nc4_info->root_grp, nc->nc4_info->root_grp); - free(nc->nc4_info); - nc4_file_list_del(nc); - } - - SUMMARIZE_ERR; - printf("*** testing file list code..."); - - { - NC_FILE_INFO_T *nc; - - if (nc4_find_nc_file(0)) ERR; - if (nc4_file_list_add(0x00000000)) ERR; - if (nc4_file_list_add(0x00010000)) ERR; - if (nc4_file_list_add(0x00020000)) ERR; - if (!(nc = nc4_find_nc_file(0x00010000))) ERR; - nc4_file_list_del(nc); - if (nc4_find_nc_file(0x00010000)) ERR; - if (!(nc = nc4_find_nc_file(0x00020000))) ERR; - nc4_file_list_del(nc); - if (nc4_find_nc_file(0x00020000)) ERR; - if (!(nc = nc4_find_nc_file(0))) ERR; - if (nc4_nc4f_list_add(nc, NAME, NC_NETCDF4)) ERR; - if (!nc->nc4_info) ERR; - if (strncmp(NAME, nc->nc4_info->path, NC_MAX_NAME)) ERR; - if (!nc->nc4_info->root_grp) ERR; - if (nc->nc4_info->root_grp->children) ERR; - if (nc->nc4_info->root_grp->next) ERR; - if (nc->nc4_info->root_grp->parent) ERR; - if (!(nc = nc4_find_nc_file(0x00000000))) ERR; - nc4_rec_grp_del(&nc->nc4_info->root_grp, nc->nc4_info->root_grp); - free(nc->nc4_info); - nc4_file_list_del(nc); - } - - SUMMARIZE_ERR; - printf("*** testing attribute list code..."); - -#define TYPE_NAME1 "crazy" -#define TYPE_NAME2 "silly" - - { - NC_ATT_INFO_T *att = NULL, **attlist = &att; - - if (nc4_att_list_add(attlist)) ERR; - nc4_att_list_del(attlist, *attlist); - } - - SUMMARIZE_ERR; - printf("*** testing type list code..."); - -#define TYPE_NAME1 "crazy" -#define TYPE_NAME2 "silly" - - { -/* NC_TYPE_INFO_T *typelist = NULL, *type;*/ - -/* if (nc4_type_list_add(&typelist, 1, 4, TYPE_NAME1)) ERR; - if (nc4_find_type(typelist, 1, &type)) ERR; - if (strcmp(type->name, TYPE_NAME1) || type->nc_typeid != 1) ERR; - type_list_del(&typelist, type); - if (nc4_type_list_add(&typelist, 2, 4, TYPE_NAME2)) ERR; - if (nc4_find_type(typelist, 2, &type)) ERR; - if (strcmp(type->name, TYPE_NAME2) || type->nc_typeid != 2) ERR; - type_list_del(&typelist, type);*/ - } - - SUMMARIZE_ERR; - - /* Print out our number of errors, if any, and exit badly. */ - if (total_err) - { - printf("%d errors detected! Sorry!\n", total_err); - return 2; - } - - printf("*** Tests successful!\n"); - return 0; -}