// // Iterate through load commands // const load_command *MachOBase::nextCommand(const load_command *command) const { using LowLevelMemoryUtilities::increment; command = increment<const load_command>(command, flip(command->cmdsize)); if (command >= mEndCommands) // end of load commands return NULL; if (increment(command, sizeof(load_command)) > mEndCommands || increment(command, flip(command->cmdsize)) > mEndCommands) UnixError::throwMe(ENOEXEC); return command; }
void *SensitiveAllocator::realloc(void *addr, size_t newSize) throw(std::bad_alloc) { size_t oldSize = malloc_size(addr); if (newSize < oldSize) memset(increment(addr, newSize), 0, oldSize - newSize); return DefaultAllocator::realloc(addr, newSize); }
void CSPFullPluginSession::Writer::use(size_t used) { assert(used <= currentSize); written += used; if (used < currentSize) { currentBuffer = increment(currentBuffer, used); currentSize -= used; } else { if (vec < lastVec) { useData(vec++); // use next vector buffer } else if (vec == lastVec && remData) { useData(remData); // use remainder buffer vec++; // mark used #if !defined(NDEBUG) && 0 } else if (vec == lastVec) { vec++; } else if (vec > lastVec) { assert(false); // 2nd try to overflow end #endif /* !NDEBUG */ } else { currentBuffer = NULL; // no more output buffer currentSize = 0; } } }
void FileDesc::writeAll(const void *addr, size_t length) { while (length > 0) { size_t size = write(addr, length); addr = increment(addr, size); length -= size; } }
// // Memory allocators for CssmHeap objects. // This implementation stores a pointer to the allocator used into memory // *after* the object's proper storage block. This allows the usual free() // functions to safely free our (hidden) pointer without knowing about it. // An allocator argument of NULL is interpreted as the standard allocator. // void *CssmHeap::operator new (size_t size, Allocator *alloc) throw(std::bad_alloc) { if (alloc == NULL) alloc = &Allocator::standard(); size = alignUp(size, alignof_template<Allocator *>()); size_t totalSize = size + sizeof(Allocator *); void *addr = alloc->malloc(totalSize); *(Allocator **)increment(addr, size) = alloc; return addr; }
// // Waiting (repeating) I/O // size_t FileDesc::readAll(void *addr, size_t length) { size_t total = 0; while (length > 0 && !atEnd()) { size_t size = read(addr, length); addr = increment(addr, size); length -= size; total += size; } return total; }
void CSPFullPluginSession::Writer::put(void *addr, size_t size) { while (size > 0) { void *p; size_t sz; nextBlock(p, sz); if (size < sz) sz = size; // cap transfer memcpy(p, addr, sz); use(sz); addr = increment(addr, sz); size -= sz; } }
void CssmHeap::operator delete (void *addr, size_t size) throw() { void *end = increment(addr, alignUp(size, alignof_template<Allocator *>())); (*(Allocator **)end)->free(addr); }