void *cmsMem_realloc(void *origBuf, UINT32 size) { void *buf; UINT32 origSize, origAllocSize, origAllocFlags; UINT32 allocSize; UINT32 *intBuf; if (origBuf == NULL) { cmsLog_error("cannot take a NULL buffer"); return NULL; } if (size == 0) { cmsMem_free(origBuf); return NULL; } allocSize = REAL_ALLOC_SIZE(size); intBuf = (UINT32 *) (((UINT32) origBuf) - CMS_MEM_HEADER_LENGTH); origAllocFlags = intBuf[0]; origSize = intBuf[1]; /* sanity check the original length */ if (intBuf[1] != (intBuf[2] ^ 0xffffffff)) { cmsLog_error("memory underflow detected, %d %d", intBuf[1], intBuf[2]); cmsAst_assert(0); return NULL; } origAllocSize = REAL_ALLOC_SIZE(origSize); if (allocSize <= origAllocSize) { /* currently, I don't shrink buffers, but could in the future. */ return origBuf; } buf = cmsMem_alloc(allocSize, origAllocFlags); if (buf != NULL) { /* got new buffer, copy orig buffer to new buffer */ memcpy(buf, origBuf, origSize); cmsMem_free(origBuf); } else { /* * We could not allocate a bigger buffer. * Return NULL but leave the original buffer untouched. */ } return buf; }
CmsRet cmsEid_getStringNamesFromBitMask(UINT16 bitMask, char **buf) { UINT32 i, mask; UINT32 len=1; /* for empty mask, return a buffer with an empty string. */ UINT32 idx=0; UINT32 numBits = sizeof(bitMask) * 8; const CmsEntityInfo *info; UBOOL8 firstName=TRUE; CmsRet ret = CMSRET_SUCCESS; /* first loop calculates how much space needed for string names */ for (mask=1, i=0; i < numBits; i++) { info = cmsEid_getEntityInfoByAccessBit(bitMask & mask); if (info == NULL) { if (bitMask & mask) { cmsLog_error("unrecognized bitmask 0x%x", (bitMask & mask)); ret = CMSRET_SUCCESS_UNRECOGNIZED_DATA_IGNORED; } } else { len += strlen(info->name) + 1; } mask = mask << 1; } if (((*buf) = cmsMem_alloc(len, ALLOC_ZEROIZE)) == NULL) { return CMSRET_RESOURCE_EXCEEDED; } /* this loop copies string names into buffer */ for (mask=1, i=0; i < numBits; i++) { info = cmsEid_getEntityInfoByAccessBit(bitMask & mask); if (info != NULL) { if (firstName) { idx = sprintf((*buf), "%s", info->name); firstName = FALSE; } else { idx += sprintf(&((*buf)[idx]), ",%s", info->name); } } mask = mask << 1; } cmsAst_assert(idx < len); return ret; }
/** Free previously allocated memory * @param buf Previously allocated buffer. */ void cmsMem_free(void *buf) { UINT32 size; if (buf != NULL) { UINT32 *intBuf = (UINT32 *) (((UINT32) buf) - CMS_MEM_HEADER_LENGTH); #ifdef CMS_MEM_LEAK_TRACING { AllocRecord *allocRec; dlist_for_each_entry(allocRec, &glbAllocRec, dlist) if (allocRec->bufAddr == buf) break; if ((DlistNode *) allocRec != &glbAllocRec) { dlist_unlink((struct dlist_node *) allocRec); free(allocRec); } else { /* * Buffers allocated from shared mem could have been freed by * another app, so if we have an alloc record but cannot find * it in shared mem, ignore it. But if the alloc record is in * private heap, that is an error. */ if (!IS_IN_SHARED_MEM(buf)) { cmsLog_error("possible double free, could not find allocRec for buf %p", buf); } } } #endif size = intBuf[1]; if (intBuf[1] != (intBuf[2] ^ 0xffffffff)) { cmsLog_error("memory underflow detected, %d %d", intBuf[1], intBuf[2]); cmsAst_assert(0); return; } #ifdef CMS_MEM_DEBUG { UINT32 allocSize, intSize, roundup4Size, i; UINT8 *charBuf = (UINT8 *) buf; allocSize = REAL_ALLOC_SIZE(intBuf[1]); intSize = allocSize / sizeof(UINT32); roundup4Size = ROUNDUP4(intBuf[1]); for (i=intBuf[1]; i < roundup4Size; i++) { if (charBuf[i] != (UINT8) (CMS_MEM_FOOTER_PATTERN & 0xff)) { cmsLog_error("memory overflow detected at idx=%d 0x%x 0x%x 0x%x", i, charBuf[i], intBuf[intSize-1], intBuf[intSize-2]); cmsAst_assert(0); return; } } if ((intBuf[intSize - 1] != CMS_MEM_FOOTER_PATTERN) || (intBuf[intSize - 2] != CMS_MEM_FOOTER_PATTERN)) { cmsLog_error("memory overflow detected, 0x%x 0x%x", intBuf[intSize - 1], intBuf[intSize - 2]); cmsAst_assert(0); return; } #ifdef CMS_MEM_POISON_ALLOC_FREE /* * write garbage into buffer which is about to be freed to detect * users of freed buffers. */ memset(intBuf, CMS_MEM_FREE_PATTERN, allocSize); #endif } #endif /* CMS_MEM_DEBUG */ buf = intBuf; /* buf points to real start of buffer */ #ifdef MDM_SHARED_MEM if (IS_IN_SHARED_MEM(buf)) { brel(buf); } else #endif { oal_free(buf); mStats.bytesAllocd -= size; mStats.numFrees++; } }
CmsRet cmsEid_getBitMaskFromStringNames(const char *buf, UINT16 *bitMask) { const char *start; const char *end; const CmsEntityInfo *info; CmsRet ret = CMSRET_SUCCESS; if (bitMask == NULL) { return CMSRET_INVALID_ARGUMENTS; } *bitMask = 0; if ((buf == NULL) || (strlen(buf) == 0)) { /* null or empty string means no bits are set */ return CMSRET_SUCCESS; } start = buf; end = strchr(start, ','); while (end != NULL) { char name[BUFLEN_256]; /* this should be long enough to hold string names */ cmsAst_assert((end - start + 1) < (SINT32) sizeof(name)); snprintf(name, end - start + 1, "%s", start); info = cmsEid_getEntityInfoByStringName(name); if (info == NULL) { cmsLog_debug("ignoring name %s", name); ret = CMSRET_SUCCESS_UNRECOGNIZED_DATA_IGNORED; } else { (*bitMask) |= info->accessBit; } start = end+1; while ((*start == ' ') && (*start != 0)) { start++; } if (*start != 0) { end = strchr(start, ','); } else { end = NULL; } } /* there is one more name at the end (with no trailing comma) */ info = cmsEid_getEntityInfoByStringName(start); if (info == NULL) { cmsLog_debug("ignoring name %s", start); ret = CMSRET_SUCCESS_UNRECOGNIZED_DATA_IGNORED; } else { (*bitMask) |= info->accessBit; } return ret; }