blob: 35e8b3229a815766181b95382f02d9e514d6587c [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#ifndef STDLIB_H
2#define STDLIB_H
3
4#include <stddef.h>
5
Uwe Hermann55e6eba2007-10-27 20:05:21 +00006#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
7
Stefan Reinauerd40393e2012-03-30 13:00:46 -07008#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1UL)
Rudolf Marek79e53252008-12-23 17:34:15 +00009#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Aaron Durbin3ece5ac2013-03-28 16:17:11 -050010#define ALIGN_UP(x,a) ALIGN((x),(a))
11#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1UL))
Andrew Bresticker19fd0b02015-02-05 13:27:59 -080012#define IS_ALIGNED(x,a) (((x) & ((typeof(x))(a)-1UL)) == 0)
Rudolf Marek79e53252008-12-23 17:34:15 +000013
Rudolf Marekc2213492008-03-19 20:24:33 +000014#define MIN(a,b) ((a) < (b) ? (a) : (b))
15#define MAX(a,b) ((a) > (b) ? (a) : (b))
Edward O'Callaghanfd570662014-07-07 20:33:09 +100016#define ABS(a) (((a) < 0) ? (-(a)) : (a))
17#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b))
18#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
Rudolf Marekc2213492008-03-19 20:24:33 +000019
Kyösti Mälkki931c1dc2014-06-30 09:40:19 +030020#define min(a,b) MIN((a),(b))
21#define max(a,b) MAX((a),(b))
22
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000023#if !defined(__PRE_RAM__)
Ron Minnicheb596362012-04-11 10:30:15 -070024void *memalign(size_t boundary, size_t size);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000025void *malloc(size_t size);
Stefan Reinauere09f7ef2012-12-18 14:27:50 -080026/* We never free memory */
27static inline void free(void *ptr) {}
Corey Osgood908ff5e2007-11-07 19:02:35 +000028#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000029
Gabe Blacke5b21272014-04-05 03:54:30 -070030#ifndef __ROMCC__
31static inline unsigned long div_round_up(unsigned int n, unsigned int d)
32{
33 return (n + d - 1) / d;
34}
35#endif
36
37
Eric Biederman8ca8d762003-04-22 19:02:15 +000038#endif /* STDLIB_H */