void* xpalloc_reallocate(XPicoAllocator* self, void* old_mem, size_t new_size) { size_t old_size = 0; void* new_mem; X_ASSERT(self); X_ASSERT(new_size > 0); if (old_mem) { char* const p = ((char*)old_mem) - X__ALIGN; X_ASSERT(x_is_aligned(old_mem, X__ALIGN)); old_size = *(size_t*)p; } new_mem = xpalloc_allocate(self, new_size); if (!new_mem) return NULL; if (!old_mem) return new_mem; /* 新しいサイズより旧いサイズの方が大きかったら新しいサイズ分コピーする。 * 古いサイズより新しいサイズの方が大きかったら古いサイズ分コピーする。 */ if (old_size > new_size) memcpy(new_mem, old_mem, new_size); else memcpy(new_mem, old_mem, old_size); xpalloc_deallocate(self, old_mem); return new_mem; }
void* xpalloc_allocate(XPicoAllocator* self, size_t size) { char* ptr; X_ASSERT(self); X_ASSERT(size > 0); /* サイズ情報確保用の領域を余分に確保する。 */ size = x_roundup_multiple(size + X__ALIGN, X__ALIGN); if (size <= sizeof(X__Chunk)) size = x_roundup_multiple(sizeof(X__Chunk) + 1, X__ALIGN); ptr = X__Allocate(self, size); X_ASSERT_MALLOC_NULL(ptr); if (ptr != NULL) { size_t used; X_ASSERT(x_is_aligned(ptr, X_ALIGN_OF(size_t))); *(size_t*)(ptr) = size; ptr += X__ALIGN; self->reserve -= size; used = self->capacity - self->reserve; if (used > self->max_used) self->max_used = used; } return ptr; }
void xspi_exchange(XSpi* self, const void* tx, void* rx, size_t size) { X_ASSERT(self); X_ASSERT(X__VFUNC(self, exchange)); X__VFUNC(self, exchange)(self->m_driver, tx, rx, size); }
XError xspi_set_format(XSpi* self, uint32_t freq_hz, XSpiMode mode, XSpiBitorder bitorder) { XError err; X_ASSERT(self); X_ASSERT(X__VFUNC(self, set_format)); err = X__VFUNC(self, set_format)(self->m_driver, freq_hz, mode, bitorder); return err; }
void xpalloc_walk_heap(const XPicoAllocator* self, XPicoAllocatorWalker walker, void* user) { const X__Chunk* chunk; X_ASSERT(self); X_ASSERT(walker); chunk = (const X__Chunk*)self->top; while (chunk) { walker((const uint8_t*)chunk, chunk->size, user); chunk = chunk->next; } }
void xspi_unlock_bus(XSpi* self) { X_ASSERT(self); if (!X__HAS_VFUNC(self, lock_bus)) return; X__VFUNC(self, lock_bus)(self->m_driver, false); }
static LockForeverArg* CreateLockForeverArg(XFiberMutex* mutex, XTicks delay) { LockForeverArg* ret = x_malloc(sizeof(LockForeverArg)); X_ASSERT(ret); ret->delay = delay; ret->mutex = mutex; return ret; }
static DestroyArg* CreateDestroyArg(XFiberMutex* mutex, XTicks delay) { DestroyArg* ret = x_malloc(sizeof(DestroyArg)); X_ASSERT(ret); ret->delay = delay; ret->mutex = mutex; return ret; }
void xpalloc_deinit(XPicoAllocator* self) { X_ASSERT(self); if (self->ownmemory) { x_free(self->heap); self->heap = NULL; self->ownmemory = false; } }
static LockArg* CreateLockArg(XFiberMutex* mutex, XTicks delay, XTicks hold_time, XError expect_err) { LockArg* ret = x_malloc(sizeof(LockArg)); X_ASSERT(ret); ret->delay = delay; ret->hold_time = hold_time; ret->mutex = mutex; ret->expect_err = expect_err; return ret; }
void xpalloc_clear(XPicoAllocator* self) { X_ASSERT(self); self->reserve = self->capacity; self->top = x_roundup_multiple_ptr(self->heap, X__ALIGN); self->max_used = 0; X__Chunk* chunk = (X__Chunk*)self->top; chunk->next = NULL; chunk->size = self->capacity; }
void xpalloc_deallocate(XPicoAllocator* self, void* ptr) { char* p; size_t size; X_ASSERT(self); if (ptr == NULL) return; /* メモリ確保の時点でアラインされてるんだから、解放メモリがアラインされてな * かったら、不正なアドレスですわな。 */ X_ASSERT(x_is_aligned(ptr, X__ALIGN)); /* 解放メモリの前にサイズ情報が仕込まれているのだ。 */ p = ((char*)ptr) - X__ALIGN; size = *(size_t*)p; X__Deallocate(self, p, size); self->reserve += size; }
bool xpalloc_init(XPicoAllocator* self, void* heap, size_t size, size_t alignment) { X__Chunk* chunk; X_ASSERT(self); X_ASSERT(alignment); X_ASSERT(x_is_power_of_two(alignment)); self->heap = heap; self->alignment = 0; self->top = NULL; self->capacity = 0; self->reserve = 0; self->max_used = 0; self->ownmemory = false; if (! heap) { heap = x_malloc(size); if (!heap) return false; self->ownmemory = true; } self->heap = heap; self->alignment = x_roundup_multiple(alignment, X_ALIGN_OF(size_t)); self->top = x_roundup_multiple_ptr(heap, X__ALIGN); X_ASSERT(self->top - self->heap >= 0); X_ASSERT(size > (size_t)(self->top - self->heap)); self->capacity = size - (self->top - self->heap); self->reserve = self->capacity; chunk = (X__Chunk*)self->top; chunk->next = NULL; chunk->size = self->capacity; return true; }
void CHttpPostRequest::_curlSetDIY() { ::curl_easy_setopt(m_pLibCURL, CURLOPT_CONNECTTIMEOUT, 10); ::curl_easy_setopt(m_pLibCURL, CURLOPT_POST, true); X_ASSERT(!m_postData.empty()); if (!m_isBinaryMode) ::curl_easy_setopt(m_pLibCURL, CURLOPT_POSTFIELDS, m_postData.c_str()); else { ::curl_easy_setopt(m_pLibCURL, CURLOPT_POSTFIELDS, m_postData.data()); ::curl_easy_setopt(m_pLibCURL, CURLOPT_POSTFIELDSIZE, m_postData.size()); } }
ErrorCode CHttpPostRequest::requestURL(const char* url, UInt32 timeoutSec) { X_ASSERT(url && *url); ErrorCode ret = EC_OK; { setURL(url); setTimeout(timeoutSec * 1000); ret = perform(); ERROR_CHECK_BOOL(EC_OK == ret); } Exit0: return ret; }
uint8_t* xpalloc_heap(const XPicoAllocator* self) { X_ASSERT(self); return self->heap; }
void xspi_init(XSpi* self) { X_ASSERT(self); X_RESET_RTTI(self); }
static void X__Deallocate(XPicoAllocator* self, void* ptr, size_t size) { X__Chunk* blk = ptr; X__Chunk* chunk = (X__Chunk*)self->top; X__Chunk* next_chunk; X_ASSERT(xpalloc_is_owner(self, ptr)); X_ASSERT((uint8_t*)ptr + size <= self->heap + self->capacity); for(;;) { next_chunk = chunk->next; if ((next_chunk == NULL) || (blk < next_chunk)) { /* 不正な解放ブロックとサイズ指定のチェック */ X_ASSERT((next_chunk == NULL) || (next_chunk >= (X__Chunk*)((uint8_t*)blk + size))); /* マージできる? */ if (blk == (X__Chunk*)((uint8_t*)chunk + chunk->size)) chunk->size += size; else if (blk < chunk) { /* マージできる? */ if (chunk == (X__Chunk*)((uint8_t*)blk + size)) { blk->next = next_chunk; blk->size = size + chunk->size; } else { blk->next = chunk; blk->size = size; } chunk = blk; /* topは常に最上位のチャンクを指す */ if (blk < (X__Chunk*)self->top) self->top = (uint8_t*)blk; } else { blk->next = next_chunk; chunk->next = blk; chunk = blk; chunk->size = size; } /* 次のチャンクとマージできる? */ if (next_chunk == (X__Chunk*)((uint8_t*)chunk + chunk->size)) { chunk->next = next_chunk->next; chunk->size += next_chunk->size; } /* メモリ解放完了!! */ break; } /* 次のループへ */ chunk = next_chunk; X_ASSERT((blk >= ((X__Chunk*)((uint8_t*)chunk + chunk->size)))); } }
size_t xpalloc_max_used(const XPicoAllocator* self) { X_ASSERT(self); return self->max_used; }
size_t xpalloc_capacity(const XPicoAllocator* self) { X_ASSERT(self); return self->capacity; }
size_t xpalloc_allocation_overhead(const XPicoAllocator* self, size_t n) { X_ASSERT(self); X_ASSERT(n > 0); return x_roundup_multiple((n + sizeof(X__Chunk)), X__ALIGN) - n; }
size_t xpalloc_reserve(const XPicoAllocator* self) { X_ASSERT(self); return self->reserve; }
void CHttpPostRequest::setPostData(std::string &postData) { X_ASSERT(!postData.empty()); m_postData = postData; }