C.Interface.And.Implementations—memory(复杂版本)的实现

2024-08-24 18:18

本文主要是介绍C.Interface.And.Implementations—memory(复杂版本)的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、After the call to  free,  p  holds a dangling pointer— a pointer that refers to memory that logically does not exist. Subse-quently dereferencing p  is an error, although if the block hasn’t been reallocated for another purpose, the error might go undetected. 

2、another error: deallocating free memory. 

3、Another error is deallocating memory that wasn’t allocated by malloc , calloc , or  realloc. 


针对以上错误,此版本memory实现较为复杂,使用了哈希表数据结构。


                  

    这个是内存的结构。整体而言用哈希表+链表进行存储。同时,释放的空间用freelist循环链表进行连接。有阴影的部分表示目前已经申请正在使用的空间。C函数中几个函数都是在这个数据结构上进行操作的。


==============================mem.h=====================================

#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "except.h"//exported exceptions
extern const Except_T Mem_Failed;//exported functions
extern void *Mem_alloc(long nbytes,const char *file, int line);
extern void *Mem_calloc(long count, long nbytes,const char *file, int line);
extern void Mem_free(void *ptr,const char *file, int line);
extern void *Mem_resize(void *ptr, long nbytes,const char *file, int line);
//exported macros
#define ALLOC(nbytes) \Mem_alloc((nbytes), __FILE__, __LINE__)
#define CALLOC(count, nbytes) \Mem_calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = ALLOC((long)sizeof *(p)))
#define NEW0(p) ((p) = CALLOC(1, (long)sizeof *(p)))
#define FREE(ptr) ((void)(Mem_free((ptr),\__FILE__, __LINE__), (ptr) = 0))
#define RESIZE(ptr, nbytes)((ptr) = Mem_resize((ptr),\(nbytes), __FILE__, __LINE__))
#endif

=============================memchk.c==================================

#include <stdlib.h>
#include <string.h>
#include "assert.h"
#include "except.h"
#include "mem.h"//checking types
union align{int i;long l;long *lp;void *p;void (*fp)(void);float f;double d;long double ld;
};//checking macros
#define hash(p, t) (((unsigned long)(p)>>3) & \(sizeof (t)/sizeof((t)[0])-1))
#define NDESCRIPTORS 512
#define NALLOC((4096 + sizeof(union align)-1)/ \(sizeof(union align)))*(sizeof (union align))//data
const Except_T Mem_Failed = { "Allocation Failed" };//checking data
static struct descriptor{struct descriptor *free;struct descriptor *link;const void *ptr;long size;const char *file;int line;
} *htab[2048];
static struct descriptor freelist = { &freelist };//checking functions
static struct descriptor *find(const void *ptr){struct descriptor *bp = htab[hash(ptr, htab)];while(bp && bp->ptr != ptr)bp = bp->link;return bp;
}void Mem_free(void *ptr, const char *file, int line){if(ptr){struct descriptor *bp;if(((unsigned long)ptr)%(sizeof (union align)) != 0|| (bp = find(ptr)) == NULL || bp->free)Except_raise(&Assert_Failed, file, line);bp->free = freelist.free;freelist.free = bp;}
}void *Mem_resize(void *ptr, long nbytes,const char *file, int line){struct descriptor *bp;void *newptr;assert(ptr);assert(nbytes > 0);if(((unsigned long)ptr)%(sizeof (union align)) != 0|| (bp = find(ptr)) == NULL || bp->free)Except_raise(&Assert_Failed, file, line);newptr = Mem_alloc(nbytes, file, line);memcpy(newptr, ptr, nbytes < bp->size ? nbytes : bp->size);Mem_free(ptr, file, line);return newptr;
}void *Mem_calloc(long count, long nbytes,const char *file, int line){void *ptr;assert(count > 0);assert(nbytes > 0);ptr = Mem_alloc(count*nbytes, file, line);memset(ptr, '\0', count*nbytes);return ptr;
}static struct descriptor *dalloc(void *ptr, long size, const char *file, int line){static struct descriptor *avail;static int nleft;if(nleft <= 0){avail = malloc(NDESCRIPTORS * sizeof (*avail));if(avail == NULL)return NULL;nleft = NDESCRIPTORS;}avail->ptr = ptr;avail->size = size;avail->file = file;avail->line = line;avail->free = avail->link = NULL;nleft--;return avail++;
}void *Mem_alloc(long nbytes, const char *file, int line){struct descriptor *bp;void *ptr;assert(nbytes > 0);//round nbytes up to an alignment boundary>nbytes = ((nbytes + sizeof(union align) - 1)/(sizeof(union align)))*(sizeof(union align));for(bp = freelist.free; bp; bp = bp->free){if(bp->size > nbytes){//use the end of the block at bp->ptrbp->size -= nbytes;ptr = (char*)bp->ptr + bp->size;if((bp = dalloc(ptr, nbytes, file, line)) != NULL){unsigned h = hash(ptr, htab);bp->link = htab[h];htab[h] = bp;return ptr;}else{if(file == NULL)RAISE(Mem_Failed);elseExcept_raise(&Mem_Failed, file, line);}}if(bp == &freelist){struct descriptor *newptr;//<newptr <- a block of size NALLOC + nbytes >if((ptr = malloc(nbytes + NALLOC)) == NULL|| (newptr = dalloc(ptr, nbytes+NALLOC,__FILE__, __LINE__)) == NULL){if(file == NULL)RAISE(Mem_Failed);elseExcept_raise(&Mem_Failed, file, line);}newptr->free = freelist.free;freelist.free = newptr;}}assert(0);return NULL;
}


这篇关于C.Interface.And.Implementations—memory(复杂版本)的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1103231

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构