drivers/ramdisk.c and include/nuttx/fs/ramdisk.h: Add logic to dispose of the drvier and RAM buffer when the RAM disk has been unlinked and all open references to the RAM disk have been closed. Add new parameters to romdisk() to specify what should be done with the RAM/ROM buffer -- Should it be freed or not? Changed all calls to ramdisk() to use these new parameters.
This commit is contained in:
parent
d8561fbcae
commit
b15632e8ba
@ -1,7 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* arch/sim/src/up_blockdevice.c
|
* arch/sim/src/up_blockdevice.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2007-2009, 2015 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -84,5 +84,6 @@
|
|||||||
|
|
||||||
void up_registerblockdevice(void)
|
void up_registerblockdevice(void)
|
||||||
{
|
{
|
||||||
ramdisk_register(0, (uint8_t*)up_deviceimage(), NSECTORS, LOGICAL_SECTOR_SIZE, true);
|
ramdisk_register(0, (uint8_t*)up_deviceimage(), NSECTORS,
|
||||||
|
LOGICAL_SECTOR_SIZE, RDFLAG_WRENABLED | RDFLAG_FUNLINK);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* arch/sim/src/up_deviceimage.c
|
* arch/sim/src/up_deviceimage.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007, 2009, 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2007, 2009, 2014-2015 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -207,107 +207,111 @@ static const unsigned char g_vfatdata[] =
|
|||||||
|
|
||||||
char *up_deviceimage(void)
|
char *up_deviceimage(void)
|
||||||
{
|
{
|
||||||
char *pbuffer;
|
char *pbuffer;
|
||||||
int bufsize = 1024*1024;
|
int bufsize = 1024*1024;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Ininitilize inflate state */
|
/* Initialize inflate state */
|
||||||
|
|
||||||
strm.zalloc = Z_NULL;
|
strm.zalloc = Z_NULL;
|
||||||
strm.zfree = Z_NULL;
|
strm.zfree = Z_NULL;
|
||||||
strm.opaque = Z_NULL;
|
strm.opaque = Z_NULL;
|
||||||
strm.avail_in = 0;
|
strm.avail_in = 0;
|
||||||
strm.next_in = Z_NULL;
|
strm.next_in = Z_NULL;
|
||||||
ret = inflateInit(&strm);
|
ret = inflateInit(&strm);
|
||||||
if (ret != Z_OK)
|
if (ret != Z_OK)
|
||||||
{
|
{
|
||||||
sdbg("inflateInit FAILED: ret=%d msg=\"%s\"\n", ret, strm.msg ? strm.msg : "No message" );
|
sdbg("inflateInit FAILED: ret=%d msg=\"%s\"\n",
|
||||||
return NULL;
|
ret, strm.msg ? strm.msg : "No message" );
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Allocate a buffer to hold the decompressed buffer. We may have
|
/* Allocate a buffer to hold the decompressed buffer. We may have to
|
||||||
* to reallocate this a few times to get the size right.
|
* reallocate this a few times to get the size right.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pbuffer = (char*)kmm_malloc(bufsize);
|
pbuffer = (char*)kmm_malloc(bufsize);
|
||||||
|
|
||||||
/* Set up the input buffer */
|
/* Set up the input buffer */
|
||||||
|
|
||||||
strm.avail_in = sizeof(g_vfatdata);
|
strm.avail_in = sizeof(g_vfatdata);
|
||||||
strm.next_in = (Bytef*)g_vfatdata;
|
strm.next_in = (Bytef*)g_vfatdata;
|
||||||
|
|
||||||
/* Run inflate() on input until output buffer not full */
|
/* Run inflate() on input until output buffer not full */
|
||||||
|
|
||||||
do {
|
do
|
||||||
/* Set up to catch the next output chunk in the output buffer */
|
{
|
||||||
|
/* Set up to catch the next output chunk in the output buffer */
|
||||||
|
|
||||||
strm.avail_out = bufsize - offset;
|
strm.avail_out = bufsize - offset;
|
||||||
strm.next_out = (Bytef*)&pbuffer[offset];
|
strm.next_out = (Bytef*)&pbuffer[offset];
|
||||||
|
|
||||||
/* inflate */
|
/* inflate */
|
||||||
|
|
||||||
ret = inflate(&strm, Z_NO_FLUSH);
|
ret = inflate(&strm, Z_NO_FLUSH);
|
||||||
|
|
||||||
/* Handle inflate() error return values */
|
/* Handle inflate() error return values */
|
||||||
|
|
||||||
switch (ret)
|
switch (ret)
|
||||||
{
|
{
|
||||||
case Z_NEED_DICT:
|
case Z_NEED_DICT:
|
||||||
case Z_DATA_ERROR:
|
case Z_DATA_ERROR:
|
||||||
case Z_MEM_ERROR:
|
case Z_MEM_ERROR:
|
||||||
case Z_STREAM_ERROR:
|
case Z_STREAM_ERROR:
|
||||||
sdbg("inflate FAILED: ret=%d msg=\"%s\"\n", ret, strm.msg ? strm.msg : "No message" );
|
sdbg("inflate FAILED: ret=%d msg=\"%s\"\n",
|
||||||
(void)inflateEnd(&strm);
|
ret, strm.msg ? strm.msg : "No message" );
|
||||||
kmm_free(pbuffer);
|
(void)inflateEnd(&strm);
|
||||||
return NULL;
|
kmm_free(pbuffer);
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* If avail_out is zero, then inflate() returned only
|
/* If avail_out is zero, then inflate() returned only because it is
|
||||||
* because it is out of buffer space. In this case, we
|
* out of buffer space. In this case, we will have to reallocate
|
||||||
* will have to reallocate the buffer and try again.
|
* the buffer and try again.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (strm.avail_out == 0)
|
if (strm.avail_out == 0)
|
||||||
{
|
{
|
||||||
int newbufsize = bufsize + 128*1024;
|
int newbufsize = bufsize + 128*1024;
|
||||||
char *newbuffer = kmm_realloc(pbuffer, newbufsize);
|
char *newbuffer = kmm_realloc(pbuffer, newbufsize);
|
||||||
if (!newbuffer)
|
if (!newbuffer)
|
||||||
{
|
{
|
||||||
kmm_free(pbuffer);
|
kmm_free(pbuffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pbuffer = newbuffer;
|
pbuffer = newbuffer;
|
||||||
offset = bufsize;
|
offset = bufsize;
|
||||||
bufsize = newbufsize;
|
bufsize = newbufsize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* There are unused bytes in the buffer, reallocate to
|
/* There are unused bytes in the buffer, reallocate to
|
||||||
* correct size.
|
* correct size.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int newbufsize = bufsize - strm.avail_out;
|
int newbufsize = bufsize - strm.avail_out;
|
||||||
char *newbuffer = kmm_realloc(pbuffer, newbufsize);
|
char *newbuffer = kmm_realloc(pbuffer, newbufsize);
|
||||||
if (!newbuffer)
|
if (!newbuffer)
|
||||||
{
|
{
|
||||||
kmm_free(pbuffer);
|
kmm_free(pbuffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pbuffer = newbuffer;
|
pbuffer = newbuffer;
|
||||||
bufsize = newbufsize;
|
bufsize = newbufsize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (strm.avail_out == 0 && ret != Z_STREAM_END);
|
}
|
||||||
|
while (strm.avail_out == 0 && ret != Z_STREAM_END);
|
||||||
|
|
||||||
(void)inflateEnd(&strm);
|
(void)inflateEnd(&strm);
|
||||||
return pbuffer;
|
return pbuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
Loading…
Reference in New Issue
Block a user