Savin Zlobec <savin@elatec.si> writes:
Now I'am thinking about a third layer to do the position ->
block/offset translation. Maybe it would be cleaner to put it into
a library since the only way I see to set block numbers is trough
set_config interface (?) .
Is there any similar effort wich will be contributed back to eCos ?
I wrote the following simplistic code:
int bytes2sectors (char *oldbuf, unsigned int bytelen, unsigned int inpos,
char **newbuf, unsigned int *sectorlen, unsigned int *outpos)
{
int sector_offset = 0;
sector_offset = inpos - (*outpos * SECTOR_SIZE);
*outpos = inpos / SECTOR_SIZE;
*sectorlen = ((bytelen + sector_offset) / SECTOR_SIZE) + 1;
*newbuf = malloc(*sectorlen * SECTOR_SIZE);
if (*newbuf == NULL) {
diag_printf("Error malloc'ing %d bytes in bytes2sectors\n",
*sectorlen * SECTOR_SIZE);
return -1;
}
return 0;
}
int sector2bytes (char *newbuf, unsigned int newlen, unsigned int newpos,
char *oldbuf, unsigned int *oldlen, unsigned int oldpos)
{
unsigned int sector_offset;
sector_offset = oldpos - (newpos * SECTOR_SIZE);
memcpy(oldbuf, newbuf + sector_offset, *oldlen);
free(newbuf);
}
Basically the idea is that you hand it a buffer that is sized in
bytes, and it hands you back a buffer that is big enough to handle the
whole thing, rounded up to blocks.