简体   繁体   English

设计和memory分配缓冲区memory

[英]Designing and memory allocation buffer memory

My application has 3 layers -downlayer, middlelayer and upperlayer.我的应用程序有 3 层 - 下层、中间层和上层。 downlayer and upperlayer are decoupled.下层和上层是解耦的。 Data passes either this way upperlayer -> middlelayer -> downlayer, or this way downlayer -> middlelayer->upperlayer.数据通过这种方式上层 -> 中间层 -> 下层,或者这种方式下层 -> 中间层 -> 上层。

My code look like this: UpperLayer.c我的代码如下所示:UpperLayer.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;
    }
}

middlelayer.h中间层.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();

middlelayer.c中间层.c

   #include<middlelayer.h>

#define COPY_MEMORY(Dst,Src,Size) memcpy(Dst,Src,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下层.c

#include<middlelayer.h> #include<中间层.h>

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;
    }
}

I call UpperWritedownData function, it calls SetFunction, then i call downReadUpData Function and read the data passed by Upper layer.我调用 UpperWritedownData function,它调用 SetFunction,然后我调用 downReadUpData Function 并读取上层传递的数据。

I want to design the middlelayer like this: you pass the data of any datatype to the middlelayer(uint8_t array can hold any datatype), during Init,the middlelayer is allocated some memory and i have the pointer of BUFFER[Up]->Data or BUFFER[DOWN]->Data and these pointers are constant and should not change throughout the program.我想这样设计中间层:您将任何数据类型的数据传递给中间层(uint8_t 数组可以保存任何数据类型),在初始化期间,中间层分配了一些 memory 并且我有BUFFER[Up]->Data的指针或BUFFER[DOWN]->Data并且这些指针是恒定的并且在整个程序中不应改变。 Now upper/down layer gives data (SetFunction), i dump it in my preallocated memory, then after some time, down/upper asks the data i copy the data from my preallocated memory and give it.Now I just need to store the recent data hence i have not implemented queues also i am overwriting and once the data is read, i dont care about it and I have to discard it.现在上/下层提供数据(SetFunction),我将其转储到我预先分配的 memory 中,然后一段时间后,下/上层询问数据我从预先分配的 memory 复制数据并给它。现在我只需要存储最近的数据,因此我没有实现队列,我也正在覆盖,一旦读取数据,我就不关心它,我必须丢弃它。

What I tried: I tried using memcpy and copy the pointer to BUFFER[x]->Data and I have commented that part of code as you can see.It did not work because memcpy copies the pointer to BUFFER[target]->Data but i wanted to copy just the contents.When I tried to use free(pointer), i get heap corruption error message.I am having trouble in using memory allocation.我尝试了什么:我尝试使用 memcpy 并将指针复制到BUFFER[x]->Data并且我已经注释了这部分代码,如您所见。它不起作用,因为 memcpy 将指针复制到BUFFER[target]->Data但我只想复制内容。当我尝试使用免费(指针)时,我收到堆损坏错误消息。我在使用 memory 分配时遇到问题。 Kindly provide inputs.请提供输入。

There is a bug in this malloc :malloc中有一个错误:

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

You define readFromdown_ptr as a pointer to DATA_ST .您将readFromdown_ptr定义为指向DATA_ST的指针。 So you want to make it point to a memory block that can hold a DATA_ST but you only allocate memory for a pointer to DATA_ST because you do sizeof(DATA_ST*) .所以你想让它指向一个可以保存DATA_ST的 memory 块,但你只为指向 DATA_ST 的指针分配DATA_ST因为你做了sizeof(DATA_ST*)

Change it to:将其更改为:

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

or better.. change it to:或更好..将其更改为:

DATA_ST* readFromdown_ptr = malloc(sizeof *readFromdown_ptr);

The same applies to the line:这同样适用于该行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM