简易内存池(下)

提示:文章

文章目录

  • 前言
  • 一、背景
  • 二、
    • 2.1
    • Ace代码
  • 三、
    • 3.1
  • 总结

前言

前期疑问:
本文目标:


一、背景

最近

二、

2.1

Ace代码

Aced代码形式如下

#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
#include "securec.h"typedef struct {int start;int end;
} MemoryUint;typedef struct {bool status;int index;
} HashMap;typedef struct {MemoryUint memoryUint[101];HashMap hashMap[101];int hashCount;int unitCount;int memLeftSize;int totalMemorySize;
} MemSystem;static int compare(const void* a, const void* b)
{return ((HashMap*) a)->index - ((HashMap*) b)->index;
}// 注意:该函数为类构造函数,返回的对象指针将作为其他待实现函数的入参;框架代码在调用该函数后,会输出 null(而非指针)
static MemSystem* MemSystemCreate(int space)
{MemSystem* sys = (MemSystem*) malloc(sizeof(MemSystem));if (sys == NULL) {return NULL;}memset_s(sys->memoryUint, sizeof(sys->memoryUint), 0, sizeof(sys->memoryUint));memset_s(sys->hashMap, sizeof(sys->hashMap), 0, sizeof(sys->hashMap));sys->totalMemorySize = space;sys->memLeftSize = space;sys->hashCount = 0;sys->unitCount = 0;return sys;
}static int MemSystemRequest(MemSystem* sys, int size)
{if(sys->totalMemorySize < size){return -1;}if (size == 0) {return -1;}if (sys->hashCount == 0) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].end + size;sys->hashMap[0].status = true;sys->hashMap[0].index = sys->memoryUint[0].start;sys->hashCount += 1;return sys->memoryUint[0].start;}for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {int spareMemSize = 0;if (sys->hashCount == 1) {if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);return sys->memoryUint[0].start;}spareMemSize = sys->totalMemorySize - sys->memoryUint[sys->hashMap[0].index].end;} else {if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);return sys->memoryUint[0].start;}int lastMemInfo = 0;if (sys->hashMap[i + 1].status == false) {lastMemInfo = sys->totalMemorySize;} else {lastMemInfo = sys->hashMap[i + 1].index;}spareMemSize = lastMemInfo - sys->memoryUint[sys->hashMap[i].index].end;}if (spareMemSize >= size) {sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start = sys->memoryUint[sys->hashMap[i].index].end;sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].end =sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start + size;if (sys->hashMap[i + 1].status == false) {sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;} else {// 在中间申请内存,hash表中间插入一个key值for (int j = sys->hashCount - 1; j > i; j--) {bool status = sys->hashMap[j].status;int index = sys->hashMap[j].index;sys->hashMap[j + 1].status = status;sys->hashMap[j + 1].index = index;}sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}sys->hashCount += 1;return sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}}}return -1;
}static bool MemSystemRelease(MemSystem* sys, int addr)
{for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {if (sys->memoryUint[sys->hashMap[i].index].start == addr) {sys->memoryUint[sys->hashMap[i].index].start = 0;sys->memoryUint[sys->hashMap[i].index].end = 0;sys->hashMap[i].status = false;sys->hashMap[i].index = 0;for (int j = i; j < sys->hashCount; j++) {bool status = sys->hashMap[j + 1].status;int index = sys->hashMap[j + 1].index;sys->hashMap[j].status = status;sys->hashMap[j].index = index;}sys->hashMap[sys->hashCount].status = false;sys->hashMap[sys->hashCount].index = 0;sys->hashCount -= 1;return true;}}}return false;
}static void MemSystemFree(MemSystem* sys)
{free(sys);
}

降低圈复杂度和嵌套深度,优化代码如下

#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
#include "securec.h"typedef struct {int start;int end;
} MemoryUint;typedef struct {bool status;int index;
} HashMap;typedef struct {MemoryUint memoryUint[101];HashMap hashMap[101];int hashCount;int unitCount;int memLeftSize;int totalMemorySize;
} MemSystem;static int compare(const void* a, const void* b)
{return ((HashMap*) a)->index - ((HashMap*) b)->index;
}static bool GetStartWhileStartAddrLargerThenRequestSize(MemSystem* sys, int size, int* start)
{if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);*start = sys->memoryUint[0].start;return true;}return false;
}static int GetStartWhileMemoryIsAvailable(MemSystem* sys, int size, int i)
{sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start = sys->memoryUint[sys->hashMap[i].index].end;sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].end =sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start + size;if (sys->hashMap[i + 1].status == false) {sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;} else {// 在中间申请内存,hash表中间插入一个key值for (int j = sys->hashCount - 1; j > i; j--) {bool status = sys->hashMap[j].status;int index = sys->hashMap[j].index;sys->hashMap[j + 1].status = status;sys->hashMap[j + 1].index = index;}sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}sys->hashCount += 1;return sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;
}// 注意:该函数为类构造函数,返回的对象指针将作为其他待实现函数的入参;框架代码在调用该函数后,会输出 null(而非指针)
static MemSystem* MemSystemCreate(int space)
{MemSystem* sys = (MemSystem*) malloc(sizeof(MemSystem));if (sys == NULL) {return NULL;}memset_s(sys->memoryUint, sizeof(sys->memoryUint), 0, sizeof(sys->memoryUint));memset_s(sys->hashMap, sizeof(sys->hashMap), 0, sizeof(sys->hashMap));sys->totalMemorySize = space;sys->memLeftSize = space;sys->hashCount = 0;sys->unitCount = 0;return sys;
}static int MemSystemRequest(MemSystem* sys, int size)
{if (sys->totalMemorySize < size) {return -1;}if (size == 0) {return -1;}if (sys->hashCount == 0) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].end + size;sys->hashMap[0].status = true;sys->hashMap[0].index = sys->memoryUint[0].start;sys->hashCount += 1;return sys->memoryUint[0].start;}for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {int spareMemSize = 0;if (sys->hashCount == 1) {int start = 0;if(GetStartWhileStartAddrLargerThenRequestSize(sys, size, &start)){return start;}spareMemSize = sys->totalMemorySize - sys->memoryUint[sys->hashMap[0].index].end;} else {int start = 0;if(GetStartWhileStartAddrLargerThenRequestSize(sys, size, &start)){return start;}int lastMemInfo = 0;if (sys->hashMap[i + 1].status == false) {lastMemInfo = sys->totalMemorySize;} else {lastMemInfo = sys->hashMap[i + 1].index;}spareMemSize = lastMemInfo - sys->memoryUint[sys->hashMap[i].index].end;}if (spareMemSize >= size) {int start = 0;start = GetStartWhileMemoryIsAvailable(sys, size, i);return start;}}}return -1;
}static bool MemSystemRelease(MemSystem* sys, int addr)
{for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {if (sys->memoryUint[sys->hashMap[i].index].start == addr) {sys->memoryUint[sys->hashMap[i].index].start = 0;sys->memoryUint[sys->hashMap[i].index].end = 0;sys->hashMap[i].status = false;sys->hashMap[i].index = 0;for (int j = i; j < sys->hashCount; j++) {bool status = sys->hashMap[j + 1].status;int index = sys->hashMap[j + 1].index;sys->hashMap[j].status = status;sys->hashMap[j].index = index;}sys->hashMap[sys->hashCount].status = false;sys->hashMap[sys->hashCount].index = 0;sys->hashCount -= 1;return true;}}}return false;
}static void MemSystemFree(MemSystem* sys)
{free(sys);
}

三、

3.1


总结

未完待续

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/64633.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

npm ERR! ECONNRESET 解决方法

问题&#xff1a;npm 命令遇到的错误是 ECONNRESET&#xff0c;这通常与网络连接问题相关。设置代理解决问题。 一、查看当前代理设置 npm config get proxy npm config get https-proxy二、设置代理 npm config set proxy http://your-proxy-address:port npm config set h…

【UE5】UnrealEngine源码构建2:windows构建unreal engine 5.3.2

参考大神知乎的文章:UE5 小白也能看懂的源码编译指南 据说会耗费400G的空间。 代码本身并不大,可能是依赖特别多,毕竟看起来UE啥都能干,核心还是c++的, 【UE5】UnrealEngine源码构建1:tag为5.3.2源码clone 本着好奇+ 学习的态度,想着也许有机会能更为深入的熟悉UE的机制…

Day60 图论part10

今天大家会感受到 Bellman_ford 算法系列在不同场景下的应用。 建议依然是:一刷的时候,能理解 原理,知道Bellman_ford 解决不同场景的问题 ,照着代码随想录能抄下来代码就好,就算达标。 二刷的时候自己尝试独立去写,三刷的时候 才能有一定深度理解各个最短路算法。 Bell…

在Linux上获取MS(如Media Server)中的RTP流并录制为双轨PCM格式的WAV文件

在Linux上获取MS(如Media Server)中的RTP流并录制为双轨PCM格式的WAV文件 一、RTP流与WAV文件格式二、实现步骤三、伪代码示例四、C语言示例代码五、关键点说明六、总结在Linux操作系统上,从媒体服务器(如Media Server,简称MS)获取RTP(Real-time Transport Protocol)流…

Vue3 简介

Vue3 简介 最新版本&#xff1a; v3.5.13 1、性能提升 打包大小减少 41% - 初次渲染快 55%, 更新渲染快 133%内存减少 54% 2、源码的升级 使用 Proxy 代替 defineProperty 实现响应式。重写虚拟 DOM 的实现和 Tree-Shaking 3、拥抱TypeScript Vue3 可以更好的支持 TypeSc…

打造RAG系统:四大向量数据库Milvus、Faiss、Elasticsearch、Chroma 全面对比与选型指南

在当今信息爆炸的时代&#xff0c;检索增强生成&#xff08;Retrieval-Augmented Generation&#xff0c;简称RAG&#xff09;系统已成为自然语言处理&#xff08;NLP&#xff09;领域的重要工具。RAG 系统通过结合生成模型和信息检索技术&#xff0c;能够在大规模数据中高效地…

检索增强生成(RAG):大语言模型的创新应用

近年来,随着自然语言处理(NLP)技术的不断发展,大型语言模型(Large Language Models, LLMs)在文本生成、对话系统等任务中展现出卓越的性能。然而,由于模型参数和训练数据的静态性,它们难以生成包含实时或领域特定信息的高质量文本。为解决这一局限性,检索增强生成(Re…

Oracle Dataguard(主库为 Oracle 11g 单节点)配置详解(1):Oracle Dataguard 概述

Oracle Dataguard&#xff08;主库为 Oracle 11g 单节点&#xff09;配置详解&#xff08;1&#xff09;&#xff1a;Oracle Dataguard 概述 目录 Oracle Dataguard&#xff08;主库为 Oracle 11g 单节点&#xff09;配置详解&#xff08;1&#xff09;&#xff1a;Oracle Data…

北京某新能源汽车生产及办公网络综合监控项目

北京某新能源汽车是某世界500强汽车集团旗下的新能源公司&#xff0c;也是国内首个获得新能源汽车生产资质、首家进行混合所有制改造、首批践行国有控股企业员工持股的新能源汽车企业&#xff0c;其主营业务包括纯电动乘用车研发设计、生产制造与销售服务。 项目现状 在企业全…

大数据系列之:深入理解学习使用腾讯COS和COS Ranger权限体系解决方案,从hdfs同步数据到cos

大数据系列之&#xff1a;深入理解学习使用腾讯COS和COS Ranger权限体系解决方案&#xff0c;从hdfs同步数据到cos 对象存储COS对象存储基本概念COS Ranger权限体系解决方案部署组件COS Ranger Plugin部署COS-Ranger-Service部署COS Ranger Client部署 COSN 从hdfs同步数据到co…

JAVA学习笔记_Redis进阶

文章目录 初识redisredis简介windows启动redis服务器linux启动redis服务器图形用户界面客户端RDM redis命令常用数据类型特殊类型字符串操作命令Key的层级格式哈希操作命令列表操作命令集合操作命令有序集合操作命令通用命令 java客户端Jedisjedis连接池SpringDataRedis序列化手…

1月第一讲:WxPython跨平台开发框架之前后端结合实现附件信息的上传及管理

1、功能描述和界面 前端&#xff08;wxPython GUI&#xff09;&#xff1a; 提供文件选择、显示文件列表的界面。支持上传、删除和下载附件。展示上传状态和附件信息&#xff08;如文件名、大小、上传时间&#xff09;。后端&#xff08;REST API 服务&#xff09;&#xff1a…

面试经典150题——滑动窗口

文章目录 1、长度最小的子数组1.1 题目链接1.2 题目描述1.3 解题代码1.4 解题思路 2、无重复字符的最长子串2.1 题目链接2.2 题目描述2.3 解题代码2.4 解题思路 3、串联所有单词的子串3.1 题目链接3.2 题目描述3.3 解题代码3.4 解题思路 4、最小覆盖子串4.1 题目链接4.2 题目描…

12.29~12.31[net][review]need to recite[part 2]

网络层 IP 首部的前一部分是固定长度&#xff0c;共 20 字节&#xff0c;是所有 IP 数据报必须具有的 路由器 路由选择协议属于网络层控制层面的内容 l 路由器 的 主要工作&#xff1a; 转发分组。 l 路由 信息协议 RIP (Routing Information Protocol ) 是 一种 分布式的…

免费下载 | 2024网络安全产业发展核心洞察与趋势预测

《2024网络安全产业发展核心洞察与趋势预测》报告的核心内容概要&#xff1a; 网络安全产业概况&#xff1a; 2023年中国网络安全产业市场规模约992亿元&#xff0c;同比增长7%。 预计2024年市场规模将增长至1091亿元&#xff0c;2025年达到1244亿元。 网络安全企业数量超过4…

Django项目部署到服务器

文章目录 django项目部署到服务器在服务器上安装Django和依赖&#xff1a;项目代码上传配置数据库收集静态文件配置Web服务器配置Gunicorn&#xff08;WSGI服务器&#xff09;启动/停止/重载systemd服务。 django项目部署到服务器 在服务器上安装Django和依赖&#xff1a; su…

记忆旅游系统|Java|SSM|VUE| 前后端分离

【技术栈】 1⃣️&#xff1a;架构: B/S、MVC 2⃣️&#xff1a;系统环境&#xff1a;Windowsh/Mac 3⃣️&#xff1a;开发环境&#xff1a;IDEA、JDK1.8、Maven、Mysql5.7 4⃣️&#xff1a;技术栈&#xff1a;Java、Mysql、SSM、Mybatis-Plus、VUE、jquery,html 5⃣️数据库可…

微信小程序:定义页面标题,动态设置页面标题,json

1、常规设置页面标题 正常微信小程序中&#xff0c;设置页面标题再json页面中进行设置&#xff0c;例如 {"usingComponents": {},"navigationBarTitleText": "标题","navigationBarBackgroundColor": "#78b7f7","navi…

基于通用优化软件GAMS的数学建模和优化分析;GAMS安装和介绍、GAMS程序编写、GAMS程序调试、实际应用算例演示与经验分享

GAMS&#xff08;General Algebraic Modeling System&#xff09;是一款高级建模系统&#xff0c;主要用于解决线性规划、非线性规划、动态规划、混合整数规划等优化问题。它以其简单清晰的用户接口和强健稳定的数值分析能力而著称&#xff0c;适用于大型、复杂的优化问题。GAM…

理解生成协同促进?华为诺亚提出ILLUME,15M数据实现多模态理解生成一体化

多模态理解与生成一体化模型&#xff0c;致力于将视觉理解与生成能力融入同一框架&#xff0c;不仅推动了任务协同与泛化能力的突破&#xff0c;更重要的是&#xff0c;它代表着对类人智能&#xff08;AGI&#xff09;的一种深层探索。通过在单一模型中统一理解与生成&#xff…