int hprofFlushRecord(hprof_record_t *rec, FILE *fp) { if (rec->dirty) { unsigned char headBuf[sizeof (u1) + 2 * sizeof (u4)]; int nb; headBuf[0] = rec->tag; U4_TO_BUF_BE(headBuf, 1, rec->time); U4_TO_BUF_BE(headBuf, 5, rec->length); nb = fwrite(headBuf, 1, sizeof(headBuf), fp); if (nb != sizeof(headBuf)) { return UNIQUE_ERROR(); } nb = fwrite(rec->body, 1, rec->length, fp); if (nb != (int)rec->length) { return UNIQUE_ERROR(); } rec->dirty = false; } //xxx if we used less than half (or whatever) of allocLen, shrink the buffer. return 0; }
static inline int guaranteeRecordAppend(hprof_record_t *rec, size_t nmore) { size_t minSize; minSize = rec->length + nmore; if (minSize > rec->allocLen) { unsigned char *newBody; size_t newAllocLen; newAllocLen = rec->allocLen * 2; if (newAllocLen < minSize) { newAllocLen = rec->allocLen + nmore + nmore/2; } newBody = (unsigned char *)realloc(rec->body, newAllocLen); if (newBody != NULL) { rec->body = newBody; rec->allocLen = newAllocLen; } else { //TODO: set an error flag so future ops will fail return UNIQUE_ERROR(); } } assert(rec->length + nmore <= rec->allocLen); return 0; }
/* *bref:创建一个class的hash表,大小是128. *retval 0:创建成功. *retval !=0:返回错误码. */ int hprofStartup_Class() { gClassHashTable = dvmHashTableCreate(128, NULL); if (gClassHashTable == NULL) { return UNIQUE_ERROR(); } return 0; }
int hprofStartNewRecord(hprof_context_t *ctx, u1 tag, u4 time) { hprof_record_t *rec = &ctx->curRec; int err; err = hprofFlushRecord(rec, ctx->memFp); if (err != 0) { return err; } else if (rec->dirty) { return UNIQUE_ERROR(); } rec->dirty = true; rec->tag = tag; rec->time = time; rec->length = 0; return 0; }