6b37de0e2b9a4f2402de85eefdc86bdc0806b1be tdreszer Tue Jan 29 14:17:41 2013 -0800 Corrected bug Angie found in previous checkin. 'Clone' is always a clone. Also changed lmBit routines to require lm, rather than suggest that it is optional. diff --git src/lib/localmem.c src/lib/localmem.c index 276b663..ff29a7e 100644 --- src/lib/localmem.c +++ src/lib/localmem.c @@ -115,50 +115,61 @@ mb = newBlock(lm, size); ret = mb->free; mb->free += ((size+lm->allignAdd)&lm->allignMask); if (mb->free > mb->end) mb->free = mb->end; return ret; } void *lmAllocMoreMem(struct lm *lm, void *pt, size_t oldSize, size_t newSize) /* Adjust memory size on a block, possibly relocating it. If block is grown, * new memory is zeroed. */ { struct lmBlock *mb = lm->blocks; // rare case that pointer is to last lm alloc, but still try. // Note this is the one place where the pointer gets reused and it is known to be in this lm -// Since lm allocs are never freed, this means the pointer CAN be reused (by lmCloneMem) if ((char *)pt + oldSize == mb->free && (char *)pt + newSize <= mb->end) { if (newSize > oldSize) // only move the free pointer on more mem mb->free = pt + newSize; return pt; } void *new = lmAlloc(lm, newSize); memcpy(new, pt, oldSize); return new; } +void *lmCloneMem(struct lm *lm, void *pt, size_t size) +/* Return a local mem copy of memory block. */ +{ +void *d = lmAlloc(lm, size); +memcpy(d, pt, size); +return d; +} + char *lmCloneStringZ(struct lm *lm, char *string, int size) /* Return local mem copy of string. */ { if (string == NULL) return NULL; -else // in rare cases, this returns the same mem - return lmAllocMoreMem(lm, string, size, size + 1); // but lm allocs are never freed so it works +else + { + char *s = lmAlloc(lm, size+1); + memcpy(s, string, size); + return s; + } } char *lmCloneFirstWord(struct lm *lm, char *line) /* Clone first word in line */ { char *startFirstWord = skipLeadingSpaces(line); if (startFirstWord == NULL) return NULL; char *endFirstWord = skipToSpaces(startFirstWord); if (endFirstWord == NULL) return lmCloneString(lm, startFirstWord); else return lmCloneStringZ(lm, startFirstWord, endFirstWord - startFirstWord); }