中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

誰能幫我做網(wǎng)站活動策劃方案

誰能幫我做網(wǎng)站,活動策劃方案,諸城網(wǎng)站建設(shè),如何用java語言做網(wǎng)站概述 在實際項目中,有些單片機資源緊缺,需要mallloc內(nèi)存,庫又沒有自帶malloc函數(shù)時,此時,就需要手動編寫,在此做個筆錄。(已在項目上使用),還可進入對齊管理機制。 直接…

概述

? ? ? ? 在實際項目中,有些單片機資源緊缺,需要mallloc內(nèi)存,庫又沒有自帶malloc函數(shù)時,此時,就需要手動編寫,在此做個筆錄。(已在項目上使用),還可進入對齊管理機制。

直接上源碼 ^_^

一、示例1:

1、mem_malloc.h文件

#ifndef __MEM_MALLOC_H__
#define __MEM_MALLOC_H__#ifdef __cplusplus
extern "C" {
#endif#include <stddef.h>void* mem_malloc(size_t size);
void mem_free(void* ptr);#ifdef __cplusplus
}
#endif#endif

2、mem_malloc.c文件

#include "mem_malloc.h"
#include "stdint.h"// 定義內(nèi)存塊結(jié)構(gòu)
typedef struct MemoryBlock {size_t size;struct MemoryBlock* next;
} MemoryBlock;// 定義內(nèi)存池起始地址
#define MEMORY_POOL_SIZE 1024
static uint8_t memoryPool[MEMORY_POOL_SIZE];// 全局指針,指向內(nèi)存池起始位置
static MemoryBlock* memoryPoolPtr = NULL;// 初始化內(nèi)存池
void memoryPoolInit(void)
{memoryPoolPtr = (MemoryBlock*)memoryPool;memoryPoolPtr->size = MEMORY_POOL_SIZE - sizeof(MemoryBlock);memoryPoolPtr->next = NULL;
}// 分配內(nèi)存
void* mem_malloc(size_t size)
{//memoryPoolInit();if (size == 0)return NULL;MemoryBlock* currentBlock = memoryPoolPtr;//MemoryBlock* prevBlock = NULL;// 遍歷內(nèi)存池中的內(nèi)存塊,找到合適大小的內(nèi)存塊while (currentBlock != NULL) {if (currentBlock->size >= size + sizeof(MemoryBlock)) {// 如果當(dāng)前內(nèi)存塊大于所需內(nèi)存,分配內(nèi)存if (currentBlock->size >= size + sizeof(MemoryBlock) + sizeof(size_t)) {MemoryBlock* newBlock = (MemoryBlock*)((uint8_t*)currentBlock + sizeof(MemoryBlock) + size);newBlock->size = currentBlock->size - sizeof(MemoryBlock) - size;newBlock->next = currentBlock->next;currentBlock->next = newBlock;currentBlock->size = size;}// 返回分配的內(nèi)存塊的地址return (uint8_t*)currentBlock + sizeof(MemoryBlock);}//prevBlock = currentBlock;currentBlock = currentBlock->next;}// 沒有足夠的內(nèi)存塊可供分配return NULL;
}// 釋放內(nèi)存
void mem_free(void* ptr)
{if (ptr == NULL)return;MemoryBlock* block = (MemoryBlock*)((uint8_t*)ptr - sizeof(MemoryBlock));block->next = memoryPoolPtr;memoryPoolPtr = block;
}// 示例代碼
int main(void) 
{// 初始化內(nèi)存池memoryPoolInit();// 分配內(nèi)存int* intPtr = (int*)mem_malloc(sizeof(int));if (intPtr != NULL) {*intPtr = 42;free(intPtr);}return 0;
}

二、示例2:

#include <stdio.h>#define MEMORY_POOL_SIZE (1024 * 2)unsigned char memoryBuff[MEMORY_POOL_SIZE];typedef struct {unsigned char* start;size_t size;
} MemoryBlock;static MemoryBlock memoryBlocks[MEMORY_POOL_SIZE] = { 0 };
static int numBlocks = 0;// 從內(nèi)存池中分配內(nèi)存
void* mem_malloc(size_t size)
{// 尋找空閑塊for (int i = 0; i < numBlocks; i++) {if (memoryBlocks[i].size == 0 && size <= MEMORY_POOL_SIZE) {// 找到合適大小的內(nèi)存塊,返回內(nèi)存塊地址memoryBlocks[i].start = memoryBuff;memoryBlocks[i].size = size;return memoryBlocks[i].start;}}// 分配新的塊if (numBlocks < MEMORY_POOL_SIZE) {memoryBlocks[numBlocks].start = memoryBuff + numBlocks;memoryBlocks[numBlocks].size = size;numBlocks++;return memoryBlocks[numBlocks - 1].start;}// 分配失敗return NULL;
}void mem_free(void* ptr)
{// 查找要釋放的塊for (int i = 0; i < numBlocks; i++) {if (memoryBlocks[i].start == ptr) {memoryBlocks[i].size = 0;break;}}
}int main(void)
{// 使用mem_malloc和mem_free進行內(nèi)存管理unsigned char* ptr1 = (unsigned char*)mem_malloc(50);unsigned char* ptr2 = (unsigned char*)mem_malloc(100);if (ptr1 != NULL && ptr2 != NULL) {// 使用分配的內(nèi)存for (int i = 0; i < 50; i++) {ptr1[i] = i;}for (int i = 0; i < 100; i++) {ptr2[i] = i + 50;}// 打印分配的內(nèi)存printf("ptr1: ");for (int i = 0; i < 50; i++) {printf("%d ", ptr1[i]);}printf("\n");printf("ptr2: ");for (int i = 0; i < 100; i++) {printf("%d ", ptr2[i]);}printf("\n");}mem_free(ptr1);mem_free(ptr2);return 0;
}

三、運行結(jié)果

四、總結(jié)

? ? ? ? 希望能幫助到你。

http://m.risenshineclean.com/news/57835.html

相關(guān)文章:

  • 企業(yè)官網(wǎng)和小程序的區(qū)別內(nèi)江seo
  • 優(yōu)秀網(wǎng)站設(shè)計欣賞圖片廣告策劃案優(yōu)秀案例
  • 南昌建站推廣公司如何優(yōu)化搜索引擎的搜索功能
  • 做美直播網(wǎng)站設(shè)計網(wǎng)頁
  • 建企業(yè)門戶網(wǎng)站刷神馬關(guān)鍵字排名軟件
  • 網(wǎng)站的空間域名如何銷售自己產(chǎn)品方法有哪些
  • 寧波做網(wǎng)站費用網(wǎng)站開發(fā)是做什么的
  • 網(wǎng)站運營心得推廣普通話手抄報模板可打印
  • 知名b2b網(wǎng)站怎么給自己的網(wǎng)站設(shè)置關(guān)鍵詞
  • 怎么做網(wǎng)站電影沈陽企業(yè)網(wǎng)站seo公司
  • 網(wǎng)站的安全度google下載app
  • 網(wǎng)站內(nèi)部資源推廣案例在線培訓(xùn)課程
  • 網(wǎng)站做代理還可以刷水嗎視頻外鏈工具
  • 國家企業(yè)信息年報系統(tǒng)優(yōu)化方案
  • 專門做nba評論的網(wǎng)站獨立站優(yōu)化
  • 做網(wǎng)站常用的css無錫網(wǎng)站優(yōu)化
  • 百度做網(wǎng)站按點擊量收費嗎品牌廣告圖片
  • 徐州靠譜網(wǎng)站開發(fā)買鏈接網(wǎng)
  • 電商設(shè)計網(wǎng)站培訓(xùn)武漢網(wǎng)絡(luò)關(guān)鍵詞排名
  • 醫(yī)院網(wǎng)站如何備案semaphore
  • h5建站是什么網(wǎng)絡(luò)輿情處置的五個步驟
  • 如何辦理網(wǎng)站備案手續(xù)怎么做網(wǎng)絡(luò)推廣最有效
  • 國家建設(shè)局網(wǎng)站app推廣怎么做
  • 視頻直播網(wǎng)站如何做廊坊seo
  • 怎么網(wǎng)上接網(wǎng)站開發(fā)單自己做百度招商加盟推廣
  • dw制造網(wǎng)站log怎么做谷歌推廣seo
  • 站長工具seo綜合查詢隱私查詢網(wǎng)推一手單渠道
  • 河北網(wǎng)站制作公司seo推廣方法
  • 網(wǎng)站建設(shè)近義詞seo sem關(guān)鍵詞優(yōu)化
  • 網(wǎng)站怎么做多級菜單引擎網(wǎng)站推廣法