Replace div_up() and round_up() functions with macros.

This commit is contained in:
Vreixo Formoso 2008-01-19 13:45:56 +01:00
parent df5aa263ec
commit 3361e941a6
9 changed files with 37 additions and 47 deletions

View File

@ -137,7 +137,7 @@ size_t calc_dir_size(Ecma119Image *t, Ecma119Node *dir, size_t *ce)
* the size of the unused space after the last directory record * the size of the unused space after the last directory record
* (ECMA-119, 6.8.1.3) * (ECMA-119, 6.8.1.3)
*/ */
len = div_up(len, BLOCK_SIZE) * BLOCK_SIZE; len = ROUND_UP(len, BLOCK_SIZE);
/* cache the len */ /* cache the len */
dir->info.dir->len = len; dir->info.dir->len = len;
@ -153,9 +153,9 @@ void calc_dir_pos(Ecma119Image *t, Ecma119Node *dir)
t->ndirs++; t->ndirs++;
dir->info.dir->block = t->curblock; dir->info.dir->block = t->curblock;
len = calc_dir_size(t, dir, &ce_len); len = calc_dir_size(t, dir, &ce_len);
t->curblock += div_up(len, BLOCK_SIZE); t->curblock += DIV_UP(len, BLOCK_SIZE);
if (t->rockridge) { if (t->rockridge) {
t->curblock += div_up(ce_len, BLOCK_SIZE); t->curblock += DIV_UP(ce_len, BLOCK_SIZE);
} }
for (i = 0; i < dir->info.dir->nchildren; i++) { for (i = 0; i < dir->info.dir->nchildren; i++) {
Ecma119Node *child = dir->info.dir->children[i]; Ecma119Node *child = dir->info.dir->children[i];
@ -212,9 +212,9 @@ int ecma119_writer_compute_data_blocks(IsoImageWriter *writer)
/* compute location for path tables */ /* compute location for path tables */
target->l_path_table_pos = target->curblock; target->l_path_table_pos = target->curblock;
target->curblock += div_up(path_table_size, BLOCK_SIZE); target->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
target->m_path_table_pos = target->curblock; target->m_path_table_pos = target->curblock;
target->curblock += div_up(path_table_size, BLOCK_SIZE); target->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
target->path_table_size = path_table_size; target->path_table_size = path_table_size;
return ISO_SUCCESS; return ISO_SUCCESS;
@ -392,7 +392,7 @@ int write_one_dir(Ecma119Image *t, Ecma119Node *dir)
memset(&info, 0, sizeof(struct susp_info)); memset(&info, 0, sizeof(struct susp_info));
if (t->rockridge) { if (t->rockridge) {
/* initialize the ce_block, it might be needed */ /* initialize the ce_block, it might be needed */
info.ce_block = dir->info.dir->block + div_up(dir->info.dir->len, info.ce_block = dir->info.dir->block + DIV_UP(dir->info.dir->len,
BLOCK_SIZE); BLOCK_SIZE);
} }

View File

@ -176,7 +176,7 @@ int filesrc_writer_compute_data_blocks(IsoImageWriter *writer)
for (i = 0; i < size; ++i) { for (i = 0; i < size; ++i) {
IsoFileSrc *file = filelist[i]; IsoFileSrc *file = filelist[i];
file->block = t->curblock; file->block = t->curblock;
t->curblock += div_up(iso_file_src_get_size(file), BLOCK_SIZE); t->curblock += DIV_UP(iso_file_src_get_size(file), BLOCK_SIZE);
} }
/* the list is only needed by this writer, store locally */ /* the list is only needed by this writer, store locally */
@ -260,10 +260,10 @@ int filesrc_writer_write_data(IsoImageWriter *writer)
/* /*
* TODO WARNING * TODO WARNING
* when we allow files greater than 4GB, current div_up implementation * when we allow files greater than 4GB, current DIV_UP implementation
* can overflow!! * can overflow!!
*/ */
uint32_t nblocks = div_up(iso_file_src_get_size(file), BLOCK_SIZE); uint32_t nblocks = DIV_UP(iso_file_src_get_size(file), BLOCK_SIZE);
res = filesrc_open(file); res = filesrc_open(file);
if (res < 0) { if (res < 0) {

View File

@ -1110,7 +1110,7 @@ int iso_file_source_new_ifs(IsoImageFilesystem *fs, IsoFileSource *parent,
/* Fill last entries */ /* Fill last entries */
atts.st_dev = fsdata->id; atts.st_dev = fsdata->id;
atts.st_blksize = BLOCK_SIZE; atts.st_blksize = BLOCK_SIZE;
atts.st_blocks = div_up(atts.st_size, BLOCK_SIZE); atts.st_blocks = DIV_UP(atts.st_size, BLOCK_SIZE);
//TODO more sanity checks!! //TODO more sanity checks!!
if (S_ISLNK(atts.st_mode) && (linkdest == NULL)) { if (S_ISLNK(atts.st_mode) && (linkdest == NULL)) {
@ -2076,7 +2076,7 @@ int create_boot_img_filesrc(IsoImageFilesystem *fs, IsoFileSource **src)
/* Fill last entries */ /* Fill last entries */
atts.st_dev = fsdata->id; atts.st_dev = fsdata->id;
atts.st_blksize = BLOCK_SIZE; atts.st_blksize = BLOCK_SIZE;
atts.st_blocks = div_up(atts.st_size, BLOCK_SIZE); atts.st_blocks = DIV_UP(atts.st_size, BLOCK_SIZE);
/* ok, we can now create the file source */ /* ok, we can now create the file source */
ifsdata = calloc(1, sizeof(ImageFileSourceData)); ifsdata = calloc(1, sizeof(ImageFileSourceData));

View File

@ -347,7 +347,7 @@ size_t calc_dir_size(Ecma119Image *t, Iso1999Node *dir)
* the size of the unused space after the last directory record * the size of the unused space after the last directory record
* (ISO 9660:1999, 6.8.1.3) * (ISO 9660:1999, 6.8.1.3)
*/ */
len = div_up(len, BLOCK_SIZE) * BLOCK_SIZE; len = ROUND_UP(len, BLOCK_SIZE);
/* cache the len */ /* cache the len */
dir->info.dir->len = len; dir->info.dir->len = len;
@ -362,7 +362,7 @@ void calc_dir_pos(Ecma119Image *t, Iso1999Node *dir)
t->iso1999_ndirs++; t->iso1999_ndirs++;
dir->info.dir->block = t->curblock; dir->info.dir->block = t->curblock;
len = calc_dir_size(t, dir); len = calc_dir_size(t, dir);
t->curblock += div_up(len, BLOCK_SIZE); t->curblock += DIV_UP(len, BLOCK_SIZE);
for (i = 0; i < dir->info.dir->nchildren; i++) { for (i = 0; i < dir->info.dir->nchildren; i++) {
Iso1999Node *child = dir->info.dir->children[i]; Iso1999Node *child = dir->info.dir->children[i];
if (child->type == ISO1999_DIR) { if (child->type == ISO1999_DIR) {
@ -419,9 +419,9 @@ int iso1999_writer_compute_data_blocks(IsoImageWriter *writer)
/* compute location for path tables */ /* compute location for path tables */
t->iso1999_l_path_table_pos = t->curblock; t->iso1999_l_path_table_pos = t->curblock;
t->curblock += div_up(path_table_size, BLOCK_SIZE); t->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
t->iso1999_m_path_table_pos = t->curblock; t->iso1999_m_path_table_pos = t->curblock;
t->curblock += div_up(path_table_size, BLOCK_SIZE); t->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
t->iso1999_path_table_size = path_table_size; t->iso1999_path_table_size = path_table_size;
return ISO_SUCCESS; return ISO_SUCCESS;

View File

@ -352,7 +352,7 @@ size_t calc_dir_size(Ecma119Image *t, JolietNode *dir)
* the size of the unused space after the last directory record * the size of the unused space after the last directory record
* (ECMA-119, 6.8.1.3) * (ECMA-119, 6.8.1.3)
*/ */
len = div_up(len, BLOCK_SIZE) * BLOCK_SIZE; len = ROUND_UP(len, BLOCK_SIZE);
/* cache the len */ /* cache the len */
dir->info.dir->len = len; dir->info.dir->len = len;
@ -367,7 +367,7 @@ void calc_dir_pos(Ecma119Image *t, JolietNode *dir)
t->joliet_ndirs++; t->joliet_ndirs++;
dir->info.dir->block = t->curblock; dir->info.dir->block = t->curblock;
len = calc_dir_size(t, dir); len = calc_dir_size(t, dir);
t->curblock += div_up(len, BLOCK_SIZE); t->curblock += DIV_UP(len, BLOCK_SIZE);
for (i = 0; i < dir->info.dir->nchildren; i++) { for (i = 0; i < dir->info.dir->nchildren; i++) {
JolietNode *child = dir->info.dir->children[i]; JolietNode *child = dir->info.dir->children[i];
if (child->type == JOLIET_DIR) { if (child->type == JOLIET_DIR) {
@ -422,9 +422,9 @@ int joliet_writer_compute_data_blocks(IsoImageWriter *writer)
/* compute location for path tables */ /* compute location for path tables */
t->joliet_l_path_table_pos = t->curblock; t->joliet_l_path_table_pos = t->curblock;
t->curblock += div_up(path_table_size, BLOCK_SIZE); t->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
t->joliet_m_path_table_pos = t->curblock; t->joliet_m_path_table_pos = t->curblock;
t->curblock += div_up(path_table_size, BLOCK_SIZE); t->curblock += DIV_UP(path_table_size, BLOCK_SIZE);
t->joliet_path_table_size = path_table_size; t->joliet_path_table_size = path_table_size;
return ISO_SUCCESS; return ISO_SUCCESS;

View File

@ -80,7 +80,7 @@ int susp_iter_next(SuspIterator *iter, struct susp_sys_user_entry **sue)
int nblocks; int nblocks;
/* A CE has found, there is another continuation area */ /* A CE has found, there is another continuation area */
nblocks = div_up(iter->ce_off + iter->ce_len, BLOCK_SIZE); nblocks = DIV_UP(iter->ce_off + iter->ce_len, BLOCK_SIZE);
iter->buffer = realloc(iter->buffer, nblocks * BLOCK_SIZE); iter->buffer = realloc(iter->buffer, nblocks * BLOCK_SIZE);
/* read all blocks needed to cache the full CE */ /* read all blocks needed to cache the full CE */

View File

@ -25,16 +25,6 @@
#define __USE_GNU #define __USE_GNU
#include <unistd.h> #include <unistd.h>
inline int div_up(unsigned int n, unsigned int div)
{
return (n + div - 1) / div;
}
inline int round_up(unsigned int n, unsigned int mul)
{
return div_up(n, mul) * mul;
}
int int_pow(int base, int power) int int_pow(int base, int power)
{ {
int result = 1; int result = 1;

View File

@ -20,8 +20,8 @@
# define MIN(a, b) (((a) < (b)) ? (a) : (b)) # define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif #endif
int div_up(unsigned int n, unsigned int div); #define DIV_UP(n,div) ((n + div - 1) / div)
int round_up(unsigned int n, unsigned int mul); #define ROUND_UP(n,mul) (DIV_UP(n, mul) * mul)
int int_pow(int base, int power); int int_pow(int base, int power);

View File

@ -55,23 +55,23 @@ static void test_strconv()
static void test_div_up() static void test_div_up()
{ {
CU_ASSERT_EQUAL( div_up(1, 2), 1 ); CU_ASSERT_EQUAL( DIV_UP(1, 2), 1 );
CU_ASSERT_EQUAL( div_up(2, 2), 1 ); CU_ASSERT_EQUAL( DIV_UP(2, 2), 1 );
CU_ASSERT_EQUAL( div_up(0, 2), 0 ); CU_ASSERT_EQUAL( DIV_UP(0, 2), 0 );
CU_ASSERT_EQUAL( div_up(-1, 2), 0 ); CU_ASSERT_EQUAL( DIV_UP(-1, 2), 0 );
CU_ASSERT_EQUAL( div_up(3, 2), 2 ); CU_ASSERT_EQUAL( DIV_UP(3, 2), 2 );
} }
static void test_round_up() static void test_round_up()
{ {
CU_ASSERT_EQUAL( round_up(1, 2), 2 ); CU_ASSERT_EQUAL( ROUND_UP(1, 2), 2 );
CU_ASSERT_EQUAL( round_up(2, 2), 2 ); CU_ASSERT_EQUAL( ROUND_UP(2, 2), 2 );
CU_ASSERT_EQUAL( round_up(0, 2), 0 ); CU_ASSERT_EQUAL( ROUND_UP(0, 2), 0 );
CU_ASSERT_EQUAL( round_up(-1, 2), 0 ); CU_ASSERT_EQUAL( ROUND_UP(-1, 2), 0 );
CU_ASSERT_EQUAL( round_up(3, 2), 4 ); CU_ASSERT_EQUAL( ROUND_UP(3, 2), 4 );
CU_ASSERT_EQUAL( round_up(15, 7), 21 ); CU_ASSERT_EQUAL( ROUND_UP(15, 7), 21 );
CU_ASSERT_EQUAL( round_up(13, 7), 14 ); CU_ASSERT_EQUAL( ROUND_UP(13, 7), 14 );
CU_ASSERT_EQUAL( round_up(14, 7), 14 ); CU_ASSERT_EQUAL( ROUND_UP(14, 7), 14 );
} }
static void test_iso_lsb_msb() static void test_iso_lsb_msb()
@ -695,8 +695,8 @@ void add_util_suite()
CU_pSuite pSuite = CU_add_suite("UtilSuite", NULL, NULL); CU_pSuite pSuite = CU_add_suite("UtilSuite", NULL, NULL);
CU_add_test(pSuite, "strconv()", test_strconv); CU_add_test(pSuite, "strconv()", test_strconv);
CU_add_test(pSuite, "div_up()", test_div_up); CU_add_test(pSuite, "DIV_UP()", test_div_up);
CU_add_test(pSuite, "round_up()", test_round_up); CU_add_test(pSuite, "ROUND_UP()", test_round_up);
CU_add_test(pSuite, "iso_bb()", test_iso_bb); CU_add_test(pSuite, "iso_bb()", test_iso_bb);
CU_add_test(pSuite, "iso_lsb/msb()", test_iso_lsb_msb); CU_add_test(pSuite, "iso_lsb/msb()", test_iso_lsb_msb);
CU_add_test(pSuite, "iso_read_lsb/msb()", test_iso_read_lsb_msb); CU_add_test(pSuite, "iso_read_lsb/msb()", test_iso_read_lsb_msb);