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

相关文章

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买