设计和内存分配缓冲存储器

问题描述

我的应用程序有 3 层 - 下层、中间层和上层。下层和上层是解耦的。数据通过上层-> 中层-> 下层,或这样下层-> 中间层-> 上层。

我的代码是这样的: 上层.c

#include <middlelayer.h>

int upperReaddownData()
{
    DATA_ST* readFromdown_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));
    if(readFromdown_ptr != NULL)
    { 
      int retVal = GetFunction(readFromdown_ptr,UP);
      if (retVal == 0) {
          bool value;
          memcpy(&value,readFromdown_ptr->continousData,sizeof(bool));
              printf("Data read %s\t",value ? "true" : "false");
              printf("\n");
          }
          // I get error here
      free(readFromdown_ptr);
      return 0;
    }
    else {
        return -1;
    }
}

int UpperWritedownData()
{
    bool  random = rand() % 2;
    DATA_ST* dumpTodown_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));
    if (dumpTodown_ptr != NULL) {
        size_t size_data = (size_t)sizeof(bool);
        memcpy(dumpTodown_ptr->continousData,&random,size_data);
        // size of data has to be mentioned while writing to memory
        dumpTodown_ptr->packetSize = size_data;
        int retVal = SetFunction(dumpTodown_ptr,DOWN);
        // i get error here
        free(dumpTodown_ptr);
        return 0;
    }
    else {
        return -1;
    }
}

中间层.h

#define UP 0x00
#define DOWN 0x01

#define BUFFER_SIZE 2

typedef struct
{
    size_t packetSize;
    uint8_t continousData[80];
}DATA_ST;

typedef struct
{
    bool isDataAlreadyRead;
    DATA_ST Data;
}PACKET_HEADER_ST;

int Init();
int GetFunction(DATA_ST* packet,uint8_t target);
int SetFunction(DATA_ST* packet,uint8_t target);
int DeInit();

中间层.c

   #include<middlelayer.h>

#define copY_MEMORY(Dst,Src,Size) memcpy(Dst,Size)

static PACKET_HEADER_ST* BUFFER[BUFFER_SIZE];

int Init()
{
    uint8_t instance;
    for (instance = 0; instance < BUFFER_SIZE; instance++) {
        BUFFER[instance] = (PACKET_HEADER_ST*)calloc(1,sizeof(PACKET_HEADER_ST));
        if (BUFFER[instance] != NULL)
        {
            BUFFER[instance]->isDataAlreadyRead = true;
        }
        else 
        {
            return -1;
        }
    }
    return 0;
}

int GetFunction(DATA_ST* packet,uint8_t target)
{
    DATA_ST* currPacket = NULL;
    if (BUFFER[target]->isDataAlreadyRead == false)
    {
        // not getting how to use memcpy properly
        //currPacket = (DATA_ST*)&BUFFER[target]->Data;
        //copY_MEMORY(packet->continousData,currPacket->continousData,sizeof(BUFFER[target]->Data.packetSize));
        //packet->packetSize = currPacket->packetSize;
        //BUFFER[target]->isDataAlreadyRead = true;
        *packet = BUFFER[target]->Data;
    }
    else
    {
        return -1;
    }
    return 0;
}

int SetFunction(DATA_ST* packet,uint8_t target)
{
    //copY_MEMORY(BUFFER[target]->Data.continousData,packet->continousData,sizeof(packet->packetSize));
    //BUFFER[target]->Data.packetSize = packet->packetSize;
    //BUFFER[target]->isDataAlreadyRead = false;
    BUFFER[target]->Data = *packet;
    BUFFER[target]->isDataAlreadyRead = false;
    return 0;
}

int DeInit()
{
    uint8_t instance;
    for (instance = 0; instance < BUFFER_SIZE; instance++) {    
        free(BUFFER[instance]);
    }
    return 0;
}

lowerlayer.c

#include

int downReadUpData(void)

{
    DATA_ST* readFromDown_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));
    if (readFromDown_ptr != NULL)
    {
        int retVal = GetFunction(readFromDown_ptr,DOWN);
        if (retVal == 0) {
            bool mybool;
            
            memcpy(&mybool,readFromDown_ptr->continousData,readFromDown_ptr->packetSize);
            // do something with data
            
        }
        // i get error here
        free(readFromDown_ptr); 
        readFromDown_ptr = NULL;
        return retVal;
    }
    else {
        return -1;
    }
}

//send dummy data to Up
int downWriteUpData(void)
{
    static bool race_b = false;
    DATA_ST* dumpToUp_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));
    if (dumpToUp_ptr != NULL) {
        size_t size_data = (size_t)sizeof(bool);
        memcpy(dumpToUp_ptr->continousData,&race_b,size_data);
        // size of data has to be mentioned while writing to memory
        dumpToUp_ptr->packetSize = size_data;
        int retVal = SetFunction(dumpToUp_ptr,UP);
        // i get error here
        free(dumpToUp_ptr);
        dumpToUp_ptr = NULL;
        race_b = !race_b;
        return retVal;
    }
    else {
        return -1;
    }
}

调用UpperWritedownData函数,它调用SetFunction,然后我调用downReadUpData函数,读取上层传过来的数据。

我想这样设计中间层:你将任何数据类型的数据传递给中间层(uint8_t数组可以容纳任何数据类型),在初始化期间,中间层被分配了一些内存,我有{{1}的指针} 或 BUFFER[Up]->Data 并且这些指针是常量,不应在整个程序中更改。现在上/下层提供数据 (SetFunction) ,我将其转储到我的预分配内存中,然后一段时间后,下/上层询问数据我从我的预分配内存中复制数据并提供它。现在我只需要存储最近的数据,因此我还没有实现队列,我正在覆盖,一旦数据被读取,我不关心它,我必须丢弃它。

我尝试过的: 我尝试使用 memcpy 并将指针复制到 BUFFER[DOWN]->Data 并且我已经注释了该部分代码,如您所见。它不起作用,因为 memcpy 将指针复制到 BUFFER[x]->Data 但我只想复制内容.当我尝试使用 free(pointer) 时,我收到堆损坏错误消息。我在使用内存分配时遇到问题。请提供意见。

解决方法

malloc 中存在错误:

DATA_ST* readFromdown_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));

您将 readFromdown_ptr 定义为指向 DATA_ST 的指针。所以你想让它指向一个可以容纳 DATA_ST 的内存块,但你只为指向 DATA_ST 的指针分配内存,因为你做了 sizeof(DATA_ST*)

改为:

DATA_ST* readFromdown_ptr = malloc(sizeof(DATA_ST));

或者更好..将其更改为:

DATA_ST* readFromdown_ptr = malloc(sizeof *readFromdown_ptr);

同样适用于该行:

DATA_ST* dumpToUp_ptr = (DATA_ST*)malloc(sizeof(DATA_ST*));