De-duplicate g_nearest_pow() implementation

And put it as static inline function into a private shared header instead.
This commit is contained in:
Sebastian Dröge 2021-11-25 14:05:42 +02:00
parent f8bfd73e30
commit 5fcd2495f9
6 changed files with 34 additions and 92 deletions

View File

@ -51,6 +51,7 @@
#include "gseekable.h" #include "gseekable.h"
#include "gioerror.h" #include "gioerror.h"
#include "gdbusprivate.h" #include "gdbusprivate.h"
#include "gutilsprivate.h"
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
#include "gunixfdlist.h" #include "gunixfdlist.h"
@ -257,17 +258,6 @@ g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf,
#define MIN_ARRAY_SIZE 128 #define MIN_ARRAY_SIZE 128
static gsize
g_nearest_pow (gsize num)
{
gsize n = 1;
while (n < num && n > 0)
n <<= 1;
return n;
}
static void static void
array_resize (GMemoryBuffer *mbuf, array_resize (GMemoryBuffer *mbuf,
gsize size) gsize size)

View File

@ -29,6 +29,7 @@
#include "gioerror.h" #include "gioerror.h"
#include "string.h" #include "string.h"
#include "glibintl.h" #include "glibintl.h"
#include "gutilsprivate.h"
/** /**
@ -596,17 +597,6 @@ array_resize (GMemoryOutputStream *ostream,
return TRUE; return TRUE;
} }
static gsize
g_nearest_pow (gsize num)
{
gsize n = 1;
while (n < num && n > 0)
n <<= 1;
return n;
}
static gssize static gssize
g_memory_output_stream_write (GOutputStream *stream, g_memory_output_stream_write (GOutputStream *stream,
const void *buffer, const void *buffer,

View File

@ -42,6 +42,7 @@
#include "gmessages.h" #include "gmessages.h"
#include "gqsort.h" #include "gqsort.h"
#include "grefcount.h" #include "grefcount.h"
#include "gutilsprivate.h"
/** /**
* SECTION:arrays * SECTION:arrays
@ -159,7 +160,6 @@ struct _GRealArray
g_array_elt_zero ((array), (array)->len, 1); \ g_array_elt_zero ((array), (array)->len, 1); \
}G_STMT_END }G_STMT_END
static gsize g_nearest_pow (gsize num) G_GNUC_CONST;
static void g_array_maybe_expand (GRealArray *array, static void g_array_maybe_expand (GRealArray *array,
guint len); guint len);
@ -972,28 +972,6 @@ g_array_binary_search (GArray *array,
return result; return result;
} }
/* Returns the smallest power of 2 greater than or equal to n,
* or 0 if such power does not fit in a gsize
*/
static gsize
g_nearest_pow (gsize num)
{
gsize n = num - 1;
g_assert (num > 0 && num <= G_MAXSIZE / 2);
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
#if GLIB_SIZEOF_SIZE_T == 8
n |= n >> 32;
#endif
return n + 1;
}
static void static void
g_array_maybe_expand (GRealArray *array, g_array_maybe_expand (GRealArray *array,
guint len) guint len)

View File

@ -37,6 +37,7 @@
#include "gstring.h" #include "gstring.h"
#include "guriprivate.h" #include "guriprivate.h"
#include "gprintf.h" #include "gprintf.h"
#include "gutilsprivate.h"
/** /**
@ -71,34 +72,13 @@
* The GString struct contains the public fields of a GString. * The GString struct contains the public fields of a GString.
*/ */
#define MY_MAXSIZE ((gsize)-1)
static inline gsize
nearest_power (gsize base, gsize num)
{
if (num > MY_MAXSIZE / 2)
{
return MY_MAXSIZE;
}
else
{
gsize n = base;
while (n < num)
n <<= 1;
return n;
}
}
static void static void
g_string_maybe_expand (GString *string, g_string_maybe_expand (GString *string,
gsize len) gsize len)
{ {
if (string->len + len >= string->allocated_len) if (string->len + len >= string->allocated_len)
{ {
string->allocated_len = nearest_power (1, string->len + len + 1); string->allocated_len = g_nearest_pow (string->len + len + 1);
string->str = g_realloc (string->str, string->allocated_len); string->str = g_realloc (string->str, string->allocated_len);
} }
} }

View File

@ -37,6 +37,7 @@
#include "gmessages.h" #include "gmessages.h"
#include "gutils.h" #include "gutils.h"
#include "gutilsprivate.h"
/** /**
* SECTION:string_chunks * SECTION:string_chunks
@ -82,27 +83,6 @@ struct _GStringChunk
gsize default_size; gsize default_size;
}; };
#define MY_MAXSIZE ((gsize)-1)
static inline gsize
nearest_power (gsize base,
gsize num)
{
if (num > MY_MAXSIZE / 2)
{
return MY_MAXSIZE;
}
else
{
gsize n = base;
while (n < num)
n <<= 1;
return n;
}
}
/** /**
* g_string_chunk_new: * g_string_chunk_new:
* @size: the default size of the blocks of memory which are * @size: the default size of the blocks of memory which are
@ -120,7 +100,7 @@ g_string_chunk_new (gsize size)
GStringChunk *new_chunk = g_new (GStringChunk, 1); GStringChunk *new_chunk = g_new (GStringChunk, 1);
gsize actual_size = 1; gsize actual_size = 1;
actual_size = nearest_power (1, size); actual_size = g_nearest_pow (MAX (1, size));
new_chunk->const_table = NULL; new_chunk->const_table = NULL;
new_chunk->storage_list = NULL; new_chunk->storage_list = NULL;
@ -280,7 +260,7 @@ g_string_chunk_insert_len (GStringChunk *chunk,
const gchar *string, const gchar *string,
gssize len) gssize len)
{ {
gssize size; gsize size;
gchar* pos; gchar* pos;
g_return_val_if_fail (chunk != NULL, NULL); g_return_val_if_fail (chunk != NULL, NULL);
@ -288,11 +268,11 @@ g_string_chunk_insert_len (GStringChunk *chunk,
if (len < 0) if (len < 0)
size = strlen (string); size = strlen (string);
else else
size = len; size = (gsize) len;
if ((chunk->storage_next + size + 1) > chunk->this_size) if ((chunk->storage_next + size + 1) > chunk->this_size)
{ {
gsize new_size = nearest_power (chunk->default_size, size + 1); gsize new_size = g_nearest_pow (MAX (chunk->default_size, size + 1));
chunk->storage_list = g_slist_prepend (chunk->storage_list, chunk->storage_list = g_slist_prepend (chunk->storage_list,
g_new (gchar, new_size)); g_new (gchar, new_size));

View File

@ -20,7 +20,9 @@
#ifndef __G_UTILS_PRIVATE_H__ #ifndef __G_UTILS_PRIVATE_H__
#define __G_UTILS_PRIVATE_H__ #define __G_UTILS_PRIVATE_H__
#include "glibconfig.h"
#include "gtypes.h" #include "gtypes.h"
#include "gtestutils.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -28,6 +30,28 @@ GLIB_AVAILABLE_IN_2_60
void g_set_user_dirs (const gchar *first_dir_type, void g_set_user_dirs (const gchar *first_dir_type,
...) G_GNUC_NULL_TERMINATED; ...) G_GNUC_NULL_TERMINATED;
/* Returns the smallest power of 2 greater than or equal to n,
* or 0 if such power does not fit in a gsize
*/
static inline gsize
g_nearest_pow (gsize num)
{
gsize n = num - 1;
g_assert (num > 0 && num <= G_MAXSIZE / 2);
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
#if GLIB_SIZEOF_SIZE_T == 8
n |= n >> 32;
#endif
return n + 1;
}
G_END_DECLS G_END_DECLS
#endif /* __G_UTILS_PRIVATE_H__ */ #endif /* __G_UTILS_PRIVATE_H__ */