mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-27 06:26:15 +01:00
delay memory allocation until after the first read. Saves a bunch of
2005-03-10 Sven Neumann <sven@gimp.org> * glib/gfileutils.c (get_contents_stdio): delay memory allocation until after the first read. Saves a bunch of reallocs. Also increased the buffer size to 4096 bytes. (bug #165954) * tests/file-test.c (test_get_contents): added a (very basic) test for g_file_get_contents().
This commit is contained in:
parent
2d7ada2898
commit
aec8923fa6
@ -1,3 +1,12 @@
|
|||||||
|
2005-03-10 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
|
* glib/gfileutils.c (get_contents_stdio): delay memory allocation
|
||||||
|
until after the first read. Saves a bunch of reallocs. Also
|
||||||
|
increased the buffer size to 4096 bytes. (bug #165954)
|
||||||
|
|
||||||
|
* tests/file-test.c (test_get_contents): added a (very basic) test
|
||||||
|
for g_file_get_contents().
|
||||||
|
|
||||||
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
||||||
|
|
||||||
* glib/glib.symbols: Add g_file_replace.
|
* glib/glib.symbols: Add g_file_replace.
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
2005-03-10 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
|
* glib/gfileutils.c (get_contents_stdio): delay memory allocation
|
||||||
|
until after the first read. Saves a bunch of reallocs. Also
|
||||||
|
increased the buffer size to 4096 bytes. (bug #165954)
|
||||||
|
|
||||||
|
* tests/file-test.c (test_get_contents): added a (very basic) test
|
||||||
|
for g_file_get_contents().
|
||||||
|
|
||||||
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
||||||
|
|
||||||
* glib/glib.symbols: Add g_file_replace.
|
* glib/glib.symbols: Add g_file_replace.
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
2005-03-10 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
|
* glib/gfileutils.c (get_contents_stdio): delay memory allocation
|
||||||
|
until after the first read. Saves a bunch of reallocs. Also
|
||||||
|
increased the buffer size to 4096 bytes. (bug #165954)
|
||||||
|
|
||||||
|
* tests/file-test.c (test_get_contents): added a (very basic) test
|
||||||
|
for g_file_get_contents().
|
||||||
|
|
||||||
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
||||||
|
|
||||||
* glib/glib.symbols: Add g_file_replace.
|
* glib/glib.symbols: Add g_file_replace.
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
2005-03-10 Sven Neumann <sven@gimp.org>
|
||||||
|
|
||||||
|
* glib/gfileutils.c (get_contents_stdio): delay memory allocation
|
||||||
|
until after the first read. Saves a bunch of reallocs. Also
|
||||||
|
increased the buffer size to 4096 bytes. (bug #165954)
|
||||||
|
|
||||||
|
* tests/file-test.c (test_get_contents): added a (very basic) test
|
||||||
|
for g_file_get_contents().
|
||||||
|
|
||||||
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
Wed Mar 9 19:06:45 2005 Manish Singh <yosh@gimp.org>
|
||||||
|
|
||||||
* glib/glib.symbols: Add g_file_replace.
|
* glib/glib.symbols: Add g_file_replace.
|
||||||
|
@ -464,33 +464,31 @@ static gboolean
|
|||||||
get_contents_stdio (const gchar *display_filename,
|
get_contents_stdio (const gchar *display_filename,
|
||||||
FILE *f,
|
FILE *f,
|
||||||
gchar **contents,
|
gchar **contents,
|
||||||
gsize *length,
|
gsize *length,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
gchar buf[2048];
|
gchar buf[4096];
|
||||||
size_t bytes;
|
size_t bytes;
|
||||||
char *str;
|
gchar *str = NULL;
|
||||||
size_t total_bytes;
|
size_t total_bytes = 0;
|
||||||
size_t total_allocated;
|
size_t total_allocated = 0;
|
||||||
|
|
||||||
g_assert (f != NULL);
|
g_assert (f != NULL);
|
||||||
|
|
||||||
#define STARTING_ALLOC 64
|
|
||||||
|
|
||||||
total_bytes = 0;
|
|
||||||
total_allocated = STARTING_ALLOC;
|
|
||||||
str = g_malloc (STARTING_ALLOC);
|
|
||||||
|
|
||||||
while (!feof (f))
|
while (!feof (f))
|
||||||
{
|
{
|
||||||
int save_errno;
|
gint save_errno;
|
||||||
|
|
||||||
bytes = fread (buf, 1, 2048, f);
|
bytes = fread (buf, 1, sizeof (buf), f);
|
||||||
save_errno = errno;
|
save_errno = errno;
|
||||||
|
|
||||||
while ((total_bytes + bytes + 1) > total_allocated)
|
while ((total_bytes + bytes + 1) > total_allocated)
|
||||||
{
|
{
|
||||||
total_allocated *= 2;
|
if (str)
|
||||||
|
total_allocated *= 2;
|
||||||
|
else
|
||||||
|
total_allocated = MIN (bytes + 1, sizeof (buf));
|
||||||
|
|
||||||
str = g_try_realloc (str, total_allocated);
|
str = g_try_realloc (str, total_allocated);
|
||||||
|
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
@ -499,13 +497,13 @@ get_contents_stdio (const gchar *display_filename,
|
|||||||
G_FILE_ERROR,
|
G_FILE_ERROR,
|
||||||
G_FILE_ERROR_NOMEM,
|
G_FILE_ERROR_NOMEM,
|
||||||
_("Could not allocate %lu bytes to read file \"%s\""),
|
_("Could not allocate %lu bytes to read file \"%s\""),
|
||||||
(gulong) total_allocated,
|
(gulong) total_allocated,
|
||||||
display_filename);
|
display_filename);
|
||||||
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ferror (f))
|
if (ferror (f))
|
||||||
{
|
{
|
||||||
g_set_error (error,
|
g_set_error (error,
|
||||||
@ -525,20 +523,20 @@ get_contents_stdio (const gchar *display_filename,
|
|||||||
fclose (f);
|
fclose (f);
|
||||||
|
|
||||||
str[total_bytes] = '\0';
|
str[total_bytes] = '\0';
|
||||||
|
|
||||||
if (length)
|
if (length)
|
||||||
*length = total_bytes;
|
*length = total_bytes;
|
||||||
|
|
||||||
*contents = str;
|
*contents = str;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
|
||||||
g_free (str);
|
g_free (str);
|
||||||
fclose (f);
|
fclose (f);
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef G_OS_WIN32
|
#ifndef G_OS_WIN32
|
||||||
|
@ -24,6 +24,7 @@ date-test
|
|||||||
dirname-test
|
dirname-test
|
||||||
env-test
|
env-test
|
||||||
file-test
|
file-test
|
||||||
|
file-test-get-contents
|
||||||
hash-test
|
hash-test
|
||||||
iochannel-test
|
iochannel-test
|
||||||
iochannel-test-outfile
|
iochannel-test-outfile
|
||||||
|
@ -131,11 +131,36 @@ test_readlink (void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_get_contents (void)
|
||||||
|
{
|
||||||
|
const gchar *text = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
const gchar *filename = "file-test-get-contents";
|
||||||
|
gchar *contents;
|
||||||
|
gsize len;
|
||||||
|
FILE *f;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
|
f = g_fopen (filename, "w");
|
||||||
|
fwrite (text, 1, strlen (text), f);
|
||||||
|
fclose (f);
|
||||||
|
|
||||||
|
g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
|
||||||
|
|
||||||
|
if (! g_file_get_contents (filename, &contents, &len, &error))
|
||||||
|
g_error ("g_file_get_contents() failed: %s", error->message);
|
||||||
|
|
||||||
|
g_assert (strcmp (text, contents) == 0 && "content mismatch");
|
||||||
|
|
||||||
|
g_free (contents);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
test_mkstemp ();
|
test_mkstemp ();
|
||||||
test_readlink ();
|
test_readlink ();
|
||||||
|
test_get_contents ();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user