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

相关文章

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

在Spring Boot中实现HTTPS加密通信及常见问题排查

《在SpringBoot中实现HTTPS加密通信及常见问题排查》HTTPS是HTTP的安全版本,通过SSL/TLS协议为通讯提供加密、身份验证和数据完整性保护,下面通过本文给大家介绍在SpringB... 目录一、HTTPS核心原理1.加密流程概述2.加密技术组合二、证书体系详解1、证书类型对比2. 证书获

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析