📄 memchunk.c
字号:
/* ---------------------------------------------------------------------------- * memchunk.c * this module contains basic functions for handling MemChunks * * Copyright 2003/2004 * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * ----------------------------------------------------------------------------*/#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <stdlib.h>#include <string.h>#include "isp-at89.h"/* This function searches for the highest address accessed in the given MemChunk. * Therefore it iterates through the MemChunks and calculates the last byte used in * each of them. The highest of those values is returned. */intmemchunk_GetProgramSize(struct MemChunk *Chunk){ int tmp, size = 0; if (Chunk) while (Chunk->header.length) { tmp = Chunk->header.address + Chunk->header.length; size = size > tmp ? size : tmp; Chunk = (struct MemChunk*) ((char*) Chunk + sizeof(struct MemChunkHeader) + Chunk->header.length); } return size;}/* This function generates a memory map of a given MemChunk. This map is a field of * flags which indicates haw each byte in the program is used. The map is the same size * as the resulting program for the microcontroller including memory areas not used. * The higest bit set in the flags means that this byte is not used in the programm. * The memory map is allocated dynamically and must be freed with free(). */struct MemoryMap*memchunk_GetMemoryMap(struct MemChunk *Chunk){ struct MemoryMap *MemMap = NULL; int datasize, n; if ((datasize = memchunk_GetProgramSize(Chunk))) { if ((MemMap = (struct MemoryMap *) malloc (sizeof(struct MemoryMap) + datasize))) { MemMap->mapsize = datasize; memset (MemMap->flags, ADR_FREE, datasize); /* mark all bytes as free */ while (Chunk->header.length) { for (n=0; n < Chunk->header.length; n++) MemMap->flags[Chunk->header.address + n] &= ~ADR_FREE; Chunk = (struct MemChunk*) ((char*) Chunk + sizeof(struct MemChunkHeader) + Chunk->header.length); } } } return MemMap;}/* This function looks for the MemChunk coming next in address space and returns * a pointer to it. If no next MemChunk exists anymore, NULL will be returned. */struct MemChunk*memchunk_FindNext(struct MemChunk *Chunk, int address){ struct MemChunk *NextChunk = NULL; if (Chunk) { while (Chunk->header.length) { if (Chunk->header.address >= address) { if (NextChunk == NULL) NextChunk = Chunk; else if (Chunk->header.address < NextChunk->header.address) NextChunk = Chunk; } Chunk = (struct MemChunk*) ((char*) Chunk + sizeof(struct MemChunkHeader) + Chunk->header.length); } } return NextChunk;}/* This function returns the chunk that contains the given address */struct MemChunk*memchunk_Find(struct MemChunk *Chunk, int address){ if (Chunk) { while (Chunk->header.length) { if (address >= Chunk->header.address && address < Chunk->header.address + Chunk->header.length) return Chunk; Chunk = (struct MemChunk*) ((char*) Chunk + sizeof(struct MemChunkHeader) + Chunk->header.length); } } return NULL;}/* This function returns an allocated memory block casted to * MemChunk or zero on error. The space for the introducing * header and the last header is added automatically. */struct MemChunk*memchunk_Alloc(int size){ struct MemChunk *buffer; buffer = (struct MemChunk *) malloc (size + 2*sizeof(struct MemChunkHeader)); return buffer;}voidmemchunk_Free(struct MemChunk *buffer){ if (buffer != NULL) free(buffer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -