📄 sds_assem.c
字号:
+ BASE_OFFSET; scp->current_heap = scp->heap + scp->shead->heap_size;}/*********************************************************************/sds_handlesds_start_heap(sds)sds_handle sds;/*********************************************************************/{ struct sds_control_p *scp = sds_control(sds); if (scp->heap != NULL) return 0L; scp->current_heap = scp->heap = (char*) sds_malloc(SDS_INC_HEAP); scp->heap_size = SDS_INC_HEAP; scp->allofl |= SDS_HEAP_ALLOC; return 1L;}/*********************************************************************/sds_handlesds_inc_heap(sds,count)sds_handle sds;int count;/*********************************************************************/{ struct sds_control_p *scp = sds_control(sds); int delta = scp->current_heap - scp->heap; scp->heap_size += count * SDS_INC_HEAP; scp->current_heap = scp->heap = sds_realloc(scp->heap,(unsigned)scp->heap_size); scp->current_heap += delta; return 1L;}/*********************************************************************/sds_off_tsds_add_to_heap(sds_handle sds,char *buffer,char delim)/*********************************************************************/{ struct sds_control_p *scp = sds_control(sds); long temp,count; int incr,space_left,size = (int)strlen(buffer) + 1; if (!scp->heap) if (!sds_start_heap(sds)) return -1L; space_left = scp->heap_size - (scp->current_heap - scp->heap); if ((incr = (size - space_left)) > 0) { incr = 1 + incr/SDS_INC_HEAP; if (!(sds_inc_heap(sds,incr))) return -1L; } count = sds_namelist(scp->current_heap,buffer,delim); temp = (scp->current_heap - scp->heap) + (count << 16); scp->current_heap += size; return temp;}/*********************************************************************/struct direc *sds_inc_direc_size(sds)sds_handle sds;/*********************************************************************/{ struct sds_control_p *scp = sds_control(sds); scp->direc_size += SDS_INC_DIREC; scp->dptr = (struct direc *)sds_realloc((char *)scp->dptr, (unsigned int)(scp->direc_size*sizeof(struct direc))); return scp->dptr;} /*********************************************************************/longtlist_len(tyl)struct type_list *tyl;/*********************************************************************/{ long len = (long)0; if (tyl == TNULL) return(len); while ((tyl++)->elemcod != SDS_ENDLIST) len++; return(++len);}/*********************************************************************/sds_handle sds_define_structure(sds,ty_list,names)sds_handle sds;struct type_list *ty_list;char *names;/*********************************************************************/{ long new_size = 0,old_size = 0; struct type_list *size_align,tlist_head; struct type_list *new_list,*ttemp = ty_list,*oldlist; struct sds_control_p *scp = sds_control(sds); int i; char align;/* clear up any bitfield definitions passed in.... */ sds_fixbits(ty_list);/* How big is the new one? (NB: delimited by SDS_ENDLIST, not SDS_RETLIST */ new_size = tlist_len(ty_list) + (long)2L;/* (A size/namelist pair will be added:, and the size/alignment type pair:hence the '+2' *//* And the old one? */ if ((oldlist = scp->tlist) != NULL) old_size = tlist_len(oldlist) - (long)1L;/* Here I subtract 1 to throw away the SDS_ENDLIST which marks the end of the type_list buffer *//* This is the first tlist entry: giving number of elements in the object and, if provided, a pointer to and length of its namelist */ tlist_head.elemcod = SDS_LENLIST; if (names != (char *)0) tlist_head.nelems = sds_add_to_heap(sds,names,','); else tlist_head.nelems = (unsigned long)0; if (old_size)/* There is an existing list, so get more space.... */ { scp->tlist = new_list = (struct type_list *)sds_realloc(scp->tlist, (unsigned)(new_size+old_size) * sizeof(struct type_list)); } else { /* No old list, simple malloc needed... */ scp->tlist = new_list = (struct type_list *)sds_malloc((unsigned long)new_size * (unsigned long)sizeof(struct type_list)); scp->allofl |= SDS_TLIST_ALLOC; }/* ...and put the new list on the end */ new_list += old_size; new_list->nelems = tlist_head.nelems; new_list->elemcod = tlist_head.elemcod; new_list++;/* the next slot is for the size/align pair, calculated soon */ size_align = new_list++; for (i=0;i<new_size - 2;i++) { new_list->nelems = ttemp->nelems; new_list->elemcod = ttemp->elemcod; if ((new_list->elemcod & SDS_LOCALLIST)) { new_list->elemcod &= ~SDS_LOCALLIST; new_list->elemcod |= SDS_INDLIST; new_list->elemcod += old_size; } new_list++; ttemp++; } old_size |= SDS_INDLIST; size_align->elemcod = SDS_SIZE_ALIGN; size_align->nelems = sds_tlsize(sds,old_size,&align); size_align->elemcod |= (long)(align & 0xff);/* Return the pointer with its indirection flag */ return(old_size);} /*********************************************************************/sds_handlesds_resize_object(sds,object,new_size)sds_handle sds;sds_code object;sds_code new_size;/*********************************************************************/{ struct direc *dptr = sds_direc(sds); sds_handle ret = 1L; if ( dptr[0].offst != SDS_NOT_ASSEMBLED) { sds_push_error(SDS_NOT_ASSEMBL,SDS_WARNING,"Resize attempted, unassembled dataset"); ret = 0L; } else if (dptr[object].nelems != new_size) { dptr[object].nelems = new_size; dptr[object].illoca |= SDS_REALLOC; } else { sds_push_error(SDS_CANNOT_RESIZE,SDS_WARNING,"Null resize attempted"); ret = 0L; } return ret;}/*********************************************************************/voidsds_destroy(sds)sds_handle sds;/*********************************************************************/{ sds_handle object; struct direc *dptr = sds_direc(sds); if (dptr == DNULL) return; if (sds_source(sds) == SDS_SHARED_MEM) {#ifdef SHMEM sds_discard(sds);#endif } else if (sds_source(sds) == SDS_FILE) { sds_discard(sds); } else { for (object = 1;object < dptr[0].nelems;object++) { if (dptr[object].illoca & SDS_WAS_ALLOCATED) free(sds_obind2ptr(sds,object)); else if (dptr[(int)object].structype == SDS_RECORDS) sds_destroy_record_def( (struct record_header *)sds_obind2ptr(sds,object),0); } sds_discard(sds); }}/*********************************************************************/voidsds_discard(sds)sds_handle sds;/*********************************************************************/{ int object_count; char ***elstart; int **varcount; char *temp; struct sds_control_p *scp = sds_control(sds); struct direc *dptr = sds_direc(sds);#ifdef MEMMAP int disob,object;#endif if (dptr == DNULL) return; temp = (char *)sds_head(sds); object_count = dptr[0].nelems;/* Clean up the three malloc regions and the serial stream and flag everything NULL*/ if (scp->source == SDS_SHARED_MEM) {#ifdef SHMEM shm_quit(dptr);#endif }#ifdef MEMMAP else if (scp->source == SDS_MAPPED_MEM) { disob = 0; /* find out if an object has been searately mapped: if not, the whole file must have been */ for (object=1; object<object_count;object++) if (dptr[object].illoca == SDS_DISJOINT_OBJECT) disob = 1; if (disob == 0 || dptr[0].illoca == SDS_DISJOINT_OBJECT) munmap((char *)sds_head(sds), sds_fullsize(sds)); else { for (object=1; object<object_count;object++) if (dptr[object].illoca == SDS_DISJOINT_OBJECT) munmap((char *)sds_obind2ptr(sds,object), dptr[object].nelems * dptr[object].elemsz); } } #endif else if (scp->source == SDS_FILE) { if ((scp->heap != NULL) && (scp->allofl & SDS_HEAP_ALLOC)) { scp->allofl &= ~SDS_HEAP_ALLOC; free(scp->heap); } free(temp); } else { if ((dptr != DNULL) && (scp->allofl & SDS_DPTR_ALLOC)) { scp->allofl &= ~SDS_DPTR_ALLOC; free((char *)dptr); } if ((scp->tlist != TNULL) && (scp->allofl & SDS_TLIST_ALLOC)) { scp->allofl &= ~SDS_TLIST_ALLOC; free((char *)scp->tlist); } if ((scp->heap != NULL) && (scp->allofl & SDS_HEAP_ALLOC)) { scp->allofl &= ~SDS_HEAP_ALLOC; free(scp->heap); } if ((scp->shead != NULL) && (scp->allofl & SDS_HEAD_ALLOC)) { scp->allofl &= ~SDS_HEAD_ALLOC; free(scp->shead); } if ((scp->dup_size != NULL) && (scp->allofl & SDS_DUP_ALLOC)) { scp->allofl &= ~SDS_DUP_ALLOC; free(scp->dup_size); } } elstart = scp->element_start; varcount = scp->varel_count; if (elstart != NULL) { int i; for (i=1;i<object_count;i++) { if (dptr[i].illoca != SDS_DISJOINT_OBJECT) { if (elstart[i] != NULL) free(elstart[i]); if (varcount[i] != NULL) free((char *)varcount[i]); } } free(elstart); free((char *)varcount); } scp->shead = NULL; scp->dup_size = NULL; scp->dptr = NULL; scp->stream = SDS_FILE_OP; scp->tlist = NULL; scp->source = 0; scp->heap = NULL; if (scp->src) sds_delete_rescon(scp->src); if (scp->load_name) free(scp->load_name); if (scp->target_name) free(scp->target_name); if (scp->file_offsets) free(scp->file_offsets); sds_delete_scp(scp);}/*********************************************************************/sds_handlesds_declare_structure(sds,obj_ptr,name,number,code)sds_handle sds;void *obj_ptr;char *name;sds_code number;sds_code code;/*********************************************************************/{ struct direc *ddptr; sds_code object; sds_handle tsz; unsigned char atype; struct sds_control_p *scp; short structype = 0; if (code & SDS_RECLIST) { code &= ~SDS_RECLIST; structype = SDS_RECORDS; } if (!(scp = sds_control(sds))) { sds_push_error(SDS_NO_SUCH_SDS, SDS_ERROR, "Adding object to non-existant dataset"); return 0L; } if (obj_ptr == SDS_ALLOCATE && scp->target_name) { sds_push_error(SDS_FILE_OP,SDS_ERROR,"Cannot allocate for targeted file"); return 0L; } /* If I can't find this thing's size, it is a. not a known primitive and b. not a described complex object. So bug off.*/ if (code & SDS_INDLIST) tsz = sds_tlsize(sds,code,(char *)&atype); else tsz = sds_element_size(sds,code,&atype); if (tsz == 0) { sds_push_error(SDS_ZERO_LENGTH, SDS_ERROR, "Adding incomplete object"); return 0L; }/* Found it. Add one to directory list.... */ ddptr = scp->dptr; object = (int)ddptr[0].nelems++;/* May have to get more memory for the directory structure */ if (object == scp->direc_size) ddptr = sds_inc_direc_size(sds);/* Fill in the definition of this object */ ddptr[object].offst = (sds_off_t)obj_ptr; ddptr[object].elemcod = code; ddptr[object].elemsz = tsz; ddptr[object].nelems = number; ddptr[object].wtime = 0; ddptr[object].illoca = 0; ddptr[object].align_type = atype; ddptr[object].structype = structype; if ((!sds_pname(sds,(long)object,name))) sds_push_error(SDS_NO_MEM, SDS_WARNING, "While adding object");/* Write to disk if requested */ if (scp->target_name != NULL) { long padbytes[2]; char b[4096]; int c = 0,s = 4096;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -