// the assumption here is that the data was either built by Deserialize() // or was created by application code in a way that observes our rule: each // struct and string was separately allocated with malloc() void FreeStruct(uint8_t *structStart, const StructMetadata *def) { if (!structStart) return; Type type; const FieldMetadata *fieldDef = NULL; for (int i = 0; i < def->nFields; i++) { fieldDef = def->fields + i; uint8_t *data = structStart + fieldDef->offset; type = (Type)(fieldDef->type & TYPE_MASK); if (TYPE_STRUCT_PTR == type) { uint8_t **p = (uint8_t**)data; FreeStruct(*p, GetStructDef(fieldDef)); } else if (TYPE_ARRAY == type) { Vec<uint8_t*> **vecPtr = (Vec<uint8_t*> **)data; Vec<uint8_t*> *vec = *vecPtr; for (size_t j = 0; j < vec->Count(); j++) { FreeStruct(vec->At(j), GetStructDef(fieldDef)); } delete vec; } else if ((TYPE_STR == type) || (TYPE_WSTR == type)) { char **sp = (char**)data; char *s = *sp; free(s); } } free(structStart); }
void FreeRequest(PRequest R) { // Уничтожаем запрос Request::Clear(R); FreeStruct(R); }
void DestroyDataHandler(LPVOID Data) { // Уничтожить данные PSendDataHandler H = (PSendDataHandler)Data; STR::Free(H->HandleURL); STR::Free(H->URL); FreeStruct(Data); }
void ResetSessionState(Vec<SessionData *> *sessionData) { CrashIf(!sessionData); for (SessionData *data : *sessionData) { FreeStruct(&gSessionDataInfo, data); } sessionData->Reset(); }
void Request::FreeList(PRequestList List) { // Уничтожить список запросов if (List == NULL) return; List::Free(List->Items); pDeleteCriticalSection(List->Lock); FreeStruct(List->Lock); }
void DeleteGlobalPrefs(GlobalPrefs *gp) { if (gp) { for (DisplayState *ds : *gp->fileStates) { delete ds->thumbnail; } FreeStruct(&gGlobalPrefsInfo, gp); } }
// Ф-ция деструктор для структуры DebugReportSettings void DebugReportFreeSettings(DebugReportSettings* settings) { if (settings == NULL) return; if (settings == &DbgRptSettingDefault) return; if (settings->StatPrefix != NULL) STR::Free(settings->StatPrefix); if (settings->StatUrl != NULL) STR::Free(settings->StatUrl); FreeStruct(settings); }
void CleanUp() { if (!gGlobalPrefs) return; for (size_t i = 0; i < gGlobalPrefs->fileStates->Count(); i++) { delete gGlobalPrefs->fileStates->At(i)->thumbnail; } FreeStruct(&gGlobalPrefsInfo, gGlobalPrefs); gGlobalPrefs = NULL; }
void Strings::Free(PStrings Strings) { // Уничтожить набор строк if (Strings == NULL) return; List::Free(PStringsRec(Strings)->Items); STR::Free(PStringsRec(Strings)->LineDelimeter); STR::Free(PStringsRec(Strings)->ValueDelimeter); FreeStruct(Strings); }
static uint8_t* DeserializeRec(DecodeState& ds, TxtNode *firstNode, TxtNode *defaultFirstNode, StructMetadata *def) { bool ok = true; if (!firstNode) return NULL; uint8_t *res = AllocArray<uint8_t>(def->size); for (int i = 0; i < def->nFields; i++) { ok = DecodeField(ds, firstNode, defaultFirstNode, def->fields + i, res); if (!ok) goto Error; } return res; Error: FreeStruct(res, def); return NULL; }
static uint8_t* DeserializeRec(DecodeState& ds, TxtNode *firstNode, const StructMetadata *def) { bool ok = true; if (!firstNode) return NULL; uint8_t *res = AllocArray<uint8_t>(def->size); const StructMetadata **defPtr = (const StructMetadata**)res; *defPtr = def; const char *fieldName = def->fieldNames; for (int i = 0; i < def->nFields; i++) { ok = DecodeField(ds, firstNode, fieldName, def->fields + i, res); if (!ok) goto Error; seqstrings::SkipStr(fieldName); } return res; Error: FreeStruct(res, def); return NULL; }
void DeleteDisplayState(DisplayState *ds) { delete ds->thumbnail; FreeStruct(&gFileStateInfo, ds); }
int main (int argc, char ** argv) { struct MainLevel demo = { (BYTE)0x88, 0xFF, (WORD)0x8844, 0xFF77, (LONG)0x88442211, 0xFF773311, 1.5, 1.75, "Hallo", { (BYTE)0x88, (LONG)0x88442211 }, /* ... */ }; BYTE b = (BYTE)0x88; WORD w = (WORD)0x8844; LONG l = (LONG)0x88442211; FLOAT f = 1.5; DOUBLE d = 1.75; STRPTR s = "Hallo"; struct Level1 l1 = { (BYTE)0x88, (LONG)0x88442211 }; BPTR fh; struct MainLevel * readback; demo.ml_BytePtr = &b; demo.ml_WordPtr = &w; demo.ml_LongPtr = &l; demo.ml_FloatPtr = &f; demo.ml_DoublePtr = &d; demo.ml_StringPtr = &s; demo.ml_Level1Ptr = &l1; fh = Open ("writestruct.dat", MODE_NEWFILE); if (!fh) { PrintFault (IoErr(), "Can't open file\n"); return 10; } /* This writes the following data stream: 0000 88 ml_Byte 0001 ff ml_Ubyte 0002 88 44 ml_Word 0004 ff 77 ml_UWord 0006 88 44 22 11 ml_Long 000a ff 77 33 11 ml_ULong 000e 3f c0 00 00 ml_Float 0012 3f fc 00 00 00 00 00 00 ml_Double 001a 01:48 61 6c 6c 6f 00 ml_String 0021 88 ml_Level1.l1_Byte 0022 88 44 22 11 ml_Level1.l1_Long 0026 01:88 ml_BytePtr 0028 01:88 44 ml_WordPtr 002b 01:88 44 22 11 ml_LongPtr 0030 01:3f c0 00 00 ml_FloatPtr 0035 01:3f fc 00 00 00 00 00 00 ml_DoublePtr 003e 01:01:48 61 6c 6c 6f 00 ml_StringPtr - Note two 01 ! 0046 01:88 88 44 22 11 ml_Level1Ptr */ if (!WriteStruct (&dsh, &demo, fh, MainDesc)) { PrintFault (IoErr(), "Failed to write to file\n"); } if (!Close (fh)) { PrintFault (IoErr(), "Failed to close file\n"); } /* Read the structure back */ fh = Open ("writestruct.dat", MODE_OLDFILE); if (!fh) { PrintFault (IoErr(), "Can't open file for reading\n"); return 10; } if (!ReadStruct (&dsh, (APTR *)&readback, fh, MainDesc)) { PrintFault (IoErr(), "Failed to read from file\n"); } else { UBYTE * ptr; int t; ptr = (UBYTE *)readback; t = 0; kprintf ("readback = %p\n", readback); kprintf ("%02X (88) %02X (FF)\n" , (UBYTE)readback->ml_Byte , readback->ml_UByte ); kprintf ("%04X (8844) %04X (FF77)\n" , (UWORD)readback->ml_Word , readback->ml_UWord ); kprintf ("%08lX (88442211) %08lX (FF773311)\n" , readback->ml_Long , readback->ml_ULong ); kprintf ("%08lX (3FC00000) %08lX:%08lX (3FFC0000:00000000)\n" , *(ULONG *)&readback->ml_Float , ((ULONG *)&readback->ml_Double)[1] , ((ULONG *)&readback->ml_Double)[0] ); kprintf ("%s (Hallo)\n" , readback->ml_String ); kprintf ("{ %02X %08X } ({ 88 88442211 })\n" , (UBYTE)readback->ml_Level1.l1_Byte , readback->ml_Level1.l1_Long ); kprintf ("%02X (88)\n" , (UBYTE)*readback->ml_BytePtr ); kprintf ("%04X (8844)\n" , (UWORD)*readback->ml_WordPtr ); kprintf ("%08lX (88442211)\n" , *readback->ml_LongPtr ); kprintf ("%08lX (3FC00000) %08lX:%08lX (3FFC0000:00000000)\n" , *(ULONG *)readback->ml_FloatPtr , ((ULONG *)readback->ml_DoublePtr)[1] , ((ULONG *)readback->ml_DoublePtr)[0] ); kprintf ("%s (Hallo)\n" , *readback->ml_StringPtr ); kprintf ("{ %02X %08X } ({ 88 88442211 })\n" , (UBYTE)readback->ml_Level1Ptr->l1_Byte , readback->ml_Level1Ptr->l1_Long ); FreeStruct (readback, MainDesc); } if (!Close (fh)) { PrintFault (IoErr(), "Failed to close file after reading\n"); } return 0; } /* main */
BOOL ReadStruct ( /* SYNOPSIS */ struct Hook * hook, APTR * dataptr, void * stream, const IPTR * sd) /* FUNCTION Reads one big endian structure from a streamhook. INPUTS hook - Streamhook dataptr - Put the data here stream - Read from this stream sd - Description of the structure to be read. The first element is the size of the structure. RESULT The function returns TRUE on success. On success, the value read is written into dataptr. On failure, FALSE is returned and the contents of dataptr are not changed. NOTES This function reads big endian values from a streamhook even on little endian machines. EXAMPLE See below. BUGS SEE ALSO ReadByte(), ReadWord(), ReadLong(), ReadFloat(), ReadDouble(), ReadString(), ReadStruct(), WriteByte(), WriteWord(), WriteLong(), WriteFloat(), WriteDouble(), WriteString(), WriteStruct() HISTORY ******************************************************************************/ { struct MinList _list; struct ReadLevel * curr; # define list ((struct List *)&_list) NEWLIST(list); if (!(curr = AllocMem (sizeof (struct ReadLevel), MEMF_ANY)) ) return FALSE; AddTail (list, (struct Node *)curr); curr->sd = sd; curr->pos = 0; curr->s = NULL; # define DESC curr->sd[curr->pos] # define IDESC curr->sd[curr->pos ++] for (;;) { if (!curr->pos) { if (!(curr->s = AllocMem (IDESC, MEMF_CLEAR)) ) goto error; } if (DESC == SDT_END) break; switch (IDESC) { case SDT_UBYTE: /* Read one 8bit byte */ if (!ReadByte (hook, (UBYTE *)(curr->s + IDESC), stream)) goto error; break; case SDT_UWORD: /* Read one 16bit word */ if (!ReadWord (hook, (UWORD *)(curr->s + IDESC), stream)) goto error; break; case SDT_ULONG: /* Read one 32bit long */ if (!ReadLong (hook, (ULONG *)(curr->s + IDESC), stream)) goto error; break; case SDT_FLOAT: /* Read one 32bit IEEE */ if (!ReadFloat (hook, (FLOAT *)(curr->s + IDESC), stream)) goto error; break; case SDT_DOUBLE: /* Read one 64bit IEEE */ if (!ReadDouble (hook, (DOUBLE *)(curr->s + IDESC), stream)) goto error; break; case SDT_STRING: { /* Read a string */ UBYTE valid_ptr; STRPTR * sptr; sptr = (STRPTR *)(curr->s + IDESC); if (!ReadByte (hook, &valid_ptr, stream)) goto error; if (valid_ptr) { if (!ReadString (hook, sptr, stream)) goto error; } else { *sptr = NULL; } break; } case SDT_STRUCT: { /* Read a structure */ struct ReadLevel * next; IPTR * desc; APTR aptr; aptr = (APTR)(curr->s + IDESC); desc = (IPTR *)IDESC; curr->pos -= 3; /* Go back to type */ if (!(next = AllocMem (sizeof (struct ReadLevel), MEMF_ANY)) ) goto error; AddTail (list, (struct Node *)next); next->sd = desc; next->pos = 1; next->s = aptr; curr = next; break; } case SDT_PTR: { /* Follow a pointer */ struct ReadLevel * next; UBYTE valid_ptr; IPTR * desc; APTR * aptr; aptr = ((APTR *)(curr->s + IDESC)); desc = (IPTR *)IDESC; if (!ReadByte (hook, &valid_ptr, stream)) goto error; if (valid_ptr) { curr->pos -= 3; if (!(next = AllocMem (sizeof (struct ReadLevel), MEMF_ANY)) ) goto error; AddTail (list, (struct Node *)next); next->sd = desc; next->pos = 0; curr = next; } else { *aptr = NULL; } break; } case SDT_IGNORE: { /* Ignore x bytes */ struct BEIOM_Ignore ig = {BEIO_IGNORE, IDESC}; if (CallHookA (hook, stream, &ig) == EOF) goto error; break; } case SDT_FILL_BYTE: { /* Fill x bytes */ IPTR offset; UBYTE value; IPTR count; offset = IDESC; value = IDESC; count = IDESC; memset (curr->s + offset, value, count); break; } case SDT_FILL_LONG: { /* Fill x longs */ ULONG * ulptr; ULONG value; IPTR count; ulptr = (ULONG *)(curr->s + IDESC); value = IDESC; count = IDESC; while (count --) *ulptr ++ = value; break; } case SDT_IFILL_BYTE: { /* Fill x bytes */ IPTR offset; UBYTE value; IPTR count; offset = IDESC; value = IDESC; count = IDESC; struct BEIOM_Ignore ig = {BEIO_IGNORE, count}; if (CallHookA (hook, stream, &ig) == EOF) goto error; memset (curr->s + offset, value, count); break; } case SDT_IFILL_LONG: { /* Fill x longs */ ULONG * ulptr; ULONG value; IPTR count; ulptr = (ULONG *)(curr->s + IDESC); value = IDESC; count = IDESC; struct BEIOM_Ignore ig = {BEIO_IGNORE, count << 2}; if (CallHookA (hook, stream, &ig) == EOF) goto error; while (count --) *ulptr ++ = value; break; } case SDT_SPECIAL: { /* Call user hook */ struct Hook * uhook; struct SDData data; data.sdd_Dest = ((APTR)(curr->s + IDESC)); data.sdd_Mode = SDV_SPECIALMODE_READ; data.sdd_Stream = stream; uhook = (struct Hook *)IDESC; if (!CallHookA (uhook, hook, &data)) goto error; break; } default: goto error; } /* switch */ /* End of the description list ? */ if (DESC == SDT_END) { struct ReadLevel * last; /* Remove the current level */ last = curr; Remove ((struct Node *)last); /* Get the last level */ if ((curr = (struct ReadLevel *)GetTail (list))) { switch (IDESC) { case SDT_STRUCT: curr->pos += 2; /* Skip 2 parameters */ break; case SDT_PTR: { APTR * aptr; aptr = ((APTR *)(curr->s + IDESC)); curr->pos ++; /* Skip description parameter */ /* Now put the result of the current level in the struct of the previous level. */ *aptr = last->s; break; } } FreeMem (last, sizeof (struct ReadLevel)); } else { curr = last; } } } /* while */ *dataptr = curr->s; FreeMem (curr, sizeof (struct ReadLevel)); return TRUE; error: curr = (struct ReadLevel *)GetHead (list); if (curr && curr->s) FreeStruct (curr->s, curr->sd); while ((curr = (struct ReadLevel *)RemTail (list))) FreeMem (curr, sizeof (struct ReadLevel)); return FALSE; } /* ReadStruct */
int MyReadRoutine(PREQUEST pReq) { // Функция полностью читает файл DWORD BufSize = 4096; LPVOID bufp = MemAlloc(BufSize); PRInt32 nbytes = 0; PRInt32 npoll; PRIntervalTime delay = (PRIntervalTime)pPR_MillisecondsToInterval(10); do { m_memset(bufp, 0, sizeof( bufp ) ); nbytes = (PRInt32)PR_ReadReal( pReq->Fd, bufp, BufSize); if ( nbytes == 0 ) return 0; if ( nbytes > 0 ) { if ( pReq->pBuf == NULL ) { if ( ( pReq->pBuf = (LPBYTE)MemAlloc( nbytes + 1 ) ) == NULL ) return -1; } else { LPBYTE p = (LPBYTE)MemRealloc( pReq->pBuf, pReq->uBufSize + nbytes + 1 ); if ( p == NULL ) return -1; pReq->pBuf = p; } m_memcpy( pReq->pBuf + pReq->uBufSize, bufp, nbytes ); pReq->uBufSize += nbytes; } if ( nbytes == -1 ) { if ( (PRErrorCode)pPR_GetError() == PR_WOULD_BLOCK_ERROR ) { PRPollDesc *pfd = CreateStruct(PRPollDesc);//(PRPollDesc*)MemAlloc( sizeof( PRPollDesc* ) ); // pfd[0].fd = pReq->Fd; // pfd[0].out_flags = 0; // pfd[0].in_flags = PR_POLL_READ; pfd->fd = pReq->Fd; pfd->out_flags = 0; pfd->in_flags = PR_POLL_READ; // npoll = (PRInt32)pPR_Poll( &pfd[0], 1, delay ); // PRInt32 OutFlags = pfd[0].out_flags; npoll = (PRInt32)pPR_Poll(pfd, 1, delay ); PRInt32 OutFlags = pfd ->out_flags; FreeStruct(pfd); // MemFree( pfd ); if ( npoll == 1 ) { if ( OutFlags & PR_POLL_READ ) continue; } break; } break; } } while ( 1 ); MemFree(bufp); if ( pReq->uBufSize == 0 ) return -1; else return 1; }
void DeleteFavorite(Favorite *fav) { FreeStruct(&gFavoriteInfo, fav); }
void FreeSimple(Simple *val) { FreeStruct((uint8_t*)val, &gSimpleMetadata); }