1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Michael Sevakis
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 int round_value_to_list32(unsigned long value
,
27 const unsigned long list
[],
31 unsigned long dmin
= ULONG_MAX
;
34 for (i
= 0; i
< count
; i
++)
44 if (signd
? ((long)list
[i
] < (long)value
) : (list
[i
] < value
))
45 diff
= value
- list
[i
];
47 diff
= list
[i
] - value
;
57 } /* round_value_to_list32 */
59 /* Number of bits set in src_mask should equal src_list length */
60 int make_list_from_caps32(unsigned long src_mask
,
61 const unsigned long *src_list
,
62 unsigned long caps_mask
,
63 unsigned long *caps_list
)
68 for (mask
= src_mask
, count
= 0, i
= 0;
72 unsigned long test_bit
;
73 mask
&= mask
- 1; /* Zero lowest bit set */
74 test_bit
= mask
^ src_mask
; /* Isolate the bit */
75 if (test_bit
& caps_mask
) /* Add item if caps has test bit set */
76 caps_list
[count
++] = src_list
? src_list
[i
] : (unsigned long)i
;
80 } /* make_list_from_caps32 */
82 /* Align a buffer and size to a size boundary while remaining within
83 * the original boundaries */
84 size_t align_buffer(void **start
, size_t size
, size_t align
)
86 void *newstart
= *start
;
87 void *newend
= newstart
+ size
;
89 /* Align the end down and the start up */
90 newend
= (void *)ALIGN_DOWN((intptr_t)newend
, align
);
91 newstart
= (void *)ALIGN_UP((intptr_t)newstart
, align
);
93 /* Hmmm - too small for this */
94 if (newend
<= newstart
)
97 /* Return adjusted pointer and size */
99 return newend
- newstart
;