Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
authorrasky <rasky@38d2e660-2303-0410-9eaa-f027e97ec537>
Sat, 14 Aug 2004 19:37:57 +0000 (19:37 +0000)
committerrasky <rasky@38d2e660-2303-0410-9eaa-f027e97ec537>
Sat, 14 Aug 2004 19:37:57 +0000 (19:37 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@141 38d2e660-2303-0410-9eaa-f027e97ec537

12 files changed:
compiler.h
cpu.h
kern/event.h
kern/msg.h
kern/proc.c
kern/proc.h
kern/proc_p.h
kern/signal.c
macros.h [new file with mode: 0755]
mware/hashtable.h
mware/heap.c
mware/pool.h [new file with mode: 0755]

index 9acdfccc85b7e5fd80d6c50b012d691c337e0d53..d0049e38186b3459e6e8233b85854e87c34eb61c 100755 (executable)
@@ -15,6 +15,9 @@
 
 /*
  * $Log$
+ * Revision 1.16  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.15  2004/08/13 03:23:26  bernie
  * Adjust a few MSVC tweaks from older projects.
  *
        #define GNUC_PREREQ(maj, min) 0
 #endif
 
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
+       #define COMPILER_C99      1
+#else
+       #define COMPILER_C99      0
+#endif
+
 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
        #pragma language=extended
        #define INTERRUPT(x)  interrupt [x]
 #endif
 
 
-/* Quasi-ANSI macros */
-#ifndef offsetof
-       /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
-       #define offsetof(s,m)   (size_t)&(((s *)0)->m)
-#endif
-#ifndef countof
-       /*! Count the number of elements in the static array \a a */
-       #define countof(a) (sizeof(a) / sizeof(*(a)))
-#endif
-
-
-/* Simple macros */
-#if GNUC_PREREQ(2,0)
-       #define ABS(n) ({ \
-               __typeof__(n) _n = (n); \
-               (_n < 0) ? -_n : _n; \
-       })
-       #define MIN(a,b) ({ \
-               __typeof__(a) _a = (a); \
-               __typeof__(b) _b = (b); \
-               (void)(&_a == &_b); /* ensure same type */ \
-               (_a < _b) ? _a : _b; \
-       })
-       #define MAX(a,b) ({ \
-               __typeof__(a) _a = (a); \
-               __typeof__(b) _b = (b); \
-               (void)(&_a == &_b); /* ensure same type */ \
-               (_a > _b) ? _a : _b; \
-       })
-#else /* !GNUC */
-       /* Buggy macros for inferior compilers */
-       #define ABS(a)          (((a) < 0) ? -(a) : (a))
-       #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
-       #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
-#endif /* !GNUC */
-
-#ifndef BV
-/*! Convert a bit value to a binary flag */
-#define BV(x)  (1<<(x))
-#endif
-
-/*! Round up \a x to an even multiple of the 2's power \a pad */
-#define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
-
-/*! Calculate a compile-time log2 for a uint8_t */
-#define UINT8_LOG2(x) \
-       ((x) < 2 ? 0 : \
-        ((x) < 4 ? 1 : \
-         ((x) < 8 ? 2 : \
-          ((x) < 16 ? 3 : \
-           ((x) < 32 ? 4 : \
-            ((x) < 64 ? 5 : \
-             ((x) < 128 ? 6 : 7)))))))
-
-/*! Calculate a compile-time log2 for a uint16_t */
-#define UINT16_LOG2(x) \
-       ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
-
-/*! Calculate a compile-time log2 for a uint32_t */
-#define UINT32_LOG2(x) \
-       ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
-
-/*! Concatenate two different preprocessor tokens (allowing macros to expand) */
-#define PP_CAT(x,y)         PP_CAT__(x,y)
-#define PP_CAT__(x,y)       x ## y
-#define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
-#define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
-#define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
-
-/*! String-ize a token (allowing macros to expand) */
-#define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
-#define PP_STRINGIZE__(x)   #x
-
-/*! Issue a compilation error if the \a condition is false */
-#define STATIC_ASSERT(condition)  \
-       extern char PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1]
-
 /*
  * Standard type definitions
  * These should be in <sys/types.h>, but many compilers lack them.
diff --git a/cpu.h b/cpu.h
index 9b77378e139b1b7af2bb0e1616a771eea33e7781..ef3e15166930b1e9e6ac66a10625ddfa29fb14ed 100755 (executable)
--- a/cpu.h
+++ b/cpu.h
@@ -17,6 +17,9 @@
 
 /*
  * $Log$
+ * Revision 1.12  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.11  2004/08/05 17:39:56  bernie
  * Fix a Doxygen tag.
  *
        #define ENABLE_INTS             do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
 
        #define DISABLE_IRQSAVE(x)  \
-               do { asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
+               do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
        #define ENABLE_IRQRESTORE(x)  \
-               do { asm(move x,SR); } while (0)
+               do { (void)x; asm(move x,SR); } while (0)
 
        typedef uint16_t cpuflags_t;
        typedef unsigned int cpustack_t;
  */
 #ifndef SCHEDULER_IDLE
        #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
-               /* This emulator hook should yeld the CPU to the host.  */
+               /* This emulator hook should yield the CPU to the host.  */
                EXTERN_C_BEGIN
                void SchedulerIdle(void);
                EXTERN_C_END
index e5826194f9c11c8c1be0f039efeb8bc4e6daf450..3a8d81507d136b2abbb9612a06e761f32c3d1b4f 100755 (executable)
@@ -18,6 +18,9 @@
 
 /*
  * $Log$
+ * Revision 1.6  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.5  2004/07/30 14:30:27  rasky
  * Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
  * Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
@@ -104,7 +107,6 @@ INLINE Event event_createNone(void)
        ((e)->action = EVENT_SOFTINT,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
 
 /*! Same as event_initSoftInt(), but returns the initialized event */
-INLINE Event event_createSoftInt(Hook func, void* user_data);
 INLINE Event event_createSoftInt(Hook func, void* user_data)
 {
        Event e;
@@ -122,11 +124,10 @@ INLINE Event event_createSoftInt(Hook func, void* user_data)
        ((e)->action = EVENT_SIGNAL,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
 
 /*! Same as event_initSignal(), but returns the initialized event */
-INLINE Event event_createSignal(struct Process* proc, sig_t bit);
 INLINE Event event_createSignal(struct Process* proc, sig_t bit)
 {
        Event e;
-       e.action = EVENT_SOFTINT;
+       e.action = EVENT_SIGNAL;
        e.Ev.Sig.sig_proc = proc;
        e.Ev.Sig.sig_bit = bit;
        return e;
index 0fcfffad45df50bb8039163b2c0e340e6e621da3..b87881b4b7fb982f0c2bc771f3bd50cbf3c47124 100755 (executable)
@@ -18,6 +18,9 @@
 
 /*
  * $Log$
+ * Revision 1.2  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.1  2004/06/06 15:11:08  bernie
  * Import into DevLib.
  *
@@ -26,6 +29,7 @@
 #define KERN_MSG_H
 
 #include "event.h"
+#include <mware/list.h>
 
 
 typedef struct MsgPort
@@ -43,17 +47,30 @@ typedef struct Msg
 } Msg;
 
 
-/*! Initialize a messge port */
-#define INITPORT(p)                    INITLIST(&(p)->queue)
+/*! Initialize a message port */
+INLINE void msg_initPort(MsgPort* port, Event event)
+{
+       INITLIST(&port->queue);
+       port->evn = event;
+}
 
-/*! Queue a message to a message port */
-#define PUTMSG(p,m) (ADDTAIL(&(p)->queue,(Node *)(m)), DOEVENT(&(p)->evn))
-#define PUTMSG_INTR(p,m) (ADDTAIL(&(p)->queue,(Node *)(m)), DOEVENT_INTR(&(p)->evn))
+/*! Queue \a msg into \a port, triggering the associated event */
+INLINE void msg_put(MsgPort* port, Msg* msg)
+{
+       ADDTAIL(&port->queue, &msg->link);
+       event_do(&port->evn);
+}
 
-/*! Get first message from port's queue (returns NULL when the port is empty) */
-#define GETMSG(p) ((Msg *)REMHEAD(&(p)->queue))
+/* Get the first message from the queue of \a port, or NULL if the port is empty */
+INLINE Msg* msg_get(MsgPort* port)
+{
+       return (Msg*)REMHEAD(&port->queue);
+}
 
-/*! Reply a message to its sender */
-#define REPLYMSG(m) (PUTMSG((m)->replyPort,(m)))
+/*! Send back (reply) \a msg to its sender */
+INLINE void msg_reply(Msg* msg)
+{
+       msg_put(msg->replyPort, msg);
+}
 
 #endif /* KERN_MSG_H */
index 6ce3071aaf104379519f69e74ec7f799ac1c5424..e0c2ba27858af55069edcde9211e6c90268815ee 100755 (executable)
@@ -17,6 +17,9 @@
 
 /*
  * $Log$
+ * Revision 1.8  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.7  2004/08/02 20:20:29  aleph
  * Merge from project_ks
  *
@@ -93,8 +96,9 @@ static void monitor_init(void)
        INITLIST(&MonitorProcs);
 }
 
-static void monitor_add(Process* proc, cpustack_t* stack_base, size_t stack_size)
+static void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
 {
+       proc->monitor.name = name;
        proc->monitor.stack_base = stack_base;
        proc->monitor.stack_size = stack_size;
 
@@ -145,13 +149,25 @@ size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
 void monitor_debug_stacks(void)
 {
        struct Process* p;
+       int i;
+
+       if (ISLISTEMPTY(&MonitorProcs))
+       {
+               kprintf("No stacks registered in the monitor\n");
+               return;
+       }
+
+       kprintf("%-24s    %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
+       for (i=0;i<56;i++)
+               kprintf("-");
+       kprintf("\n");
 
        for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
                 p->monitor.link.succ;
                 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
        {
                size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
-               kprintf("TCB: %x  sp_base: %x  sp_size: %x  sp_free: %x\n", (uint16_t)p, (uint16_t)p->monitor.stack_base, (uint16_t)p->monitor.stack_size, (uint16_t)free);
+               kprintf("%-24s    %04x    %04x    %4x    %4x\n", p->monitor.name, (uint16_t)p, (uint16_t)p->monitor.stack_base, (uint16_t)p->monitor.stack_size, (uint16_t)free);
        }
 }
 
@@ -199,7 +215,7 @@ void proc_init(void)
  * \return Process structure of new created process
  *         if successful, NULL otherwise.
  */
-Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
+struct Process *proc_new_with_name(UNUSED(const char*, name), void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack_base)
 {
        Process *proc;
        cpuflags_t flags;
@@ -258,6 +274,7 @@ Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
        }
 
        proc_init_struct(proc);
+       proc->user_data = data;
 
 #if CONFIG_KERN_HEAP
        proc->stack_base = stack_base;
@@ -280,7 +297,7 @@ Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack_base)
        ENABLE_IRQRESTORE(flags);
 
 #if CONFIG_KERN_MONITOR
-       monitor_add(proc, stack_base, stacksize);
+       monitor_add(proc, name, stack_base, stacksize);
 #endif
 
        return proc;
@@ -416,6 +433,13 @@ struct Process *proc_current(void)
        return CurrentProcess;
 }
 
+/*!
+ * Get the pointer to the user data of the current process
+ */
+IPTR proc_current_user_data(void)
+{
+       return CurrentProcess->user_data;
+}
 
 #if 0 /* Simple testcase for the scheduler */
 
@@ -453,8 +477,8 @@ static cpustack_t proc_test_stack2[CONFIG_KERN_DEFSTACKSIZE/sizeof(cpustack_t)];
  */
 void NORETURN proc_test(void)
 {
-       proc_new(proc_test_thread1, sizeof(proc_test_stack1), proc_test_stack1);
-       proc_new(proc_test_thread2, sizeof(proc_test_stack2), proc_test_stack2);
+       proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1);
+       proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2);
        kputs("Created tasks\n");
 
        kputs("stack1:\n");
index 6039c4c101cb22988f75a9c0f3032ae9e87adad0..26cbc57998965d2d7ae90397b908d27ccb0f8139 100755 (executable)
@@ -15,6 +15,9 @@
 
 /*
  * $Log$
+ * Revision 1.4  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.3  2004/07/30 14:31:23  rasky
  * Hunk sfuggito al commit precedente (aggiornamento kernel)
  *
@@ -38,11 +41,20 @@ struct Process;
 
 /* Task scheduling services */
 void proc_init(void);
-struct Process *proc_new(void (*entry)(void), size_t stacksize, cpustack_t *stack);
+struct Process *proc_new_with_name(const char* name, void (*entry)(void), IPTR data, size_t stacksize, cpustack_t *stack);
+
+#if !CONFIG_KERN_MONITOR
+       #define proc_new(entry,data,size,stack) proc_new_with_name(NULL,(entry),(data),(size),(stack))
+#else
+       #define proc_new(entry,data,size,stack) proc_new_with_name(#entry,(entry),(data),(size),(stack))
+#endif
+
 void proc_exit(void);
 void proc_switch(void);
 void proc_test(void);
 struct Process* proc_current(void);
+IPTR proc_current_user_data(void);
+
 
 #if CONFIG_KERN_MONITOR
 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size);
index 5daf0dd0d47568c7c610c308fc7152a45faa5e24..79e97b9230cbe16bd7b9cbbfafc38622cb1e1f34 100755 (executable)
@@ -15,6 +15,9 @@
 
 /*
  * $Log$
+ * Revision 1.5  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.4  2004/07/30 14:24:16  rasky
  * Task switching con salvataggio perfetto stato di interrupt (SR)
  * Kernel monitor per dump informazioni su stack dei processi
@@ -54,6 +57,7 @@ typedef struct Process
 {
        Node         link;        /*!< Link Process into scheduler lists */
        cpustack_t  *stack;       /*!< Per-process SP */
+       IPTR         user_data;   /*!< Custom data passed to the process */
 
 #if CONFIG_KERN_SIGNALS
        sigset_t     sig_wait;    /*!< Signals the process is waiting for */
@@ -70,6 +74,7 @@ typedef struct Process
        struct ProcMonitor
        {
                Node link;
+               const char* name;
                cpustack_t* stack_base;
                size_t stack_size;
        } monitor;
index 0af4f3992510ac07861bf7af9622026c0391c859..513efc28795f43ed1af1719dcba080b6e5f8d98c 100755 (executable)
@@ -66,6 +66,9 @@
 
 /*
  * $Log$
+ * Revision 1.6  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.5  2004/08/04 21:50:33  bernie
  * Add extensive documentation.
  *
@@ -89,6 +92,7 @@
 #include "proc.h"
 #include "proc_p.h"
 #include "hw.h"
+#include <drv/kdebug.h>
 
 // FIXME
 #if CONFIG_KERN_SIGNALS
@@ -127,6 +131,10 @@ sigset_t sig_wait(sigset_t sigs)
                /* go to sleep and proc_schedule() another process */
                CurrentProcess->sig_wait = sigs;
                proc_schedule();
+
+               /* When we come back here, a signal must be arrived */
+               ASSERT(!CurrentProcess->sig_wait);
+               ASSERT(CurrentProcess->sig_recv);
        }
 
        /* Signals found: clear them and return */
diff --git a/macros.h b/macros.h
new file mode 100755 (executable)
index 0000000..2d6cfb4
--- /dev/null
+++ b/macros.h
@@ -0,0 +1,212 @@
+/*!
+ * \file
+ * <!--
+ * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
+ * This file is part of DevLib - See devlib/README for information.
+ * -->
+ *
+ * \version $Id$
+ *
+ * \author Bernardo Innocenti <bernie@develer.com>
+ * \author Giovanni Bajo <rasky@develer.com>
+ *
+ * \brief Common and handy function macros
+ */
+
+/*
+ * $Log$
+ * Revision 1.1  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
+ * Revision 1.4  2004/08/14 18:36:50  rasky
+ * Doxygen fix e un livello di parentesi aggiuntivi per la macro
+ *
+ * Revision 1.3  2004/08/12 20:01:32  rasky
+ * Aggiunte macro BIT_CHANGE e BIT_CHANGE_BV
+ *
+ * Revision 1.2  2004/08/10 21:36:14  rasky
+ * Aggiunto include macros.h dove serve
+ * Aggiunta dipendenza da compiler.h in macros.h
+ *
+ * Revision 1.1  2004/08/10 21:30:00  rasky
+ * Estratte le funzioni macro in macros.h
+ *
+ */
+
+#ifndef MACROS_H
+#define MACROS_H
+
+#include <compiler.h>
+
+/* Quasi-ANSI macros */
+#ifndef offsetof
+       /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
+       #define offsetof(s,m)   (size_t)&(((s *)0)->m)
+#endif
+#ifndef countof
+       /*! Count the number of elements in the static array \a a */
+       #define countof(a)      (sizeof(a) / sizeof(*(a)))
+#endif
+
+
+/* Simple macros */
+#define ABS(a)      (((a) < 0) ? -(a) : (a))
+#define MIN(a,b)    (((a) < (b)) ? (a) : (b))
+#define MAX(a,b)    (((a) > (b)) ? (a) : (b))
+
+#ifndef BV
+/*! Convert a bit value to a binary flag */
+#define BV(x)       (1<<(x))
+#endif
+
+/*! Round up \a x to an even multiple of the 2's power \a pad */
+#define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
+
+//! Check if \a x is an integer power of 2
+#define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
+
+/*! Calculate a compile-time log2 for a uint8_t */
+#define UINT8_LOG2(x) \
+       ((x) < 2 ? 0 : \
+        ((x) < 4 ? 1 : \
+         ((x) < 8 ? 2 : \
+          ((x) < 16 ? 3 : \
+           ((x) < 32 ? 4 : \
+            ((x) < 64 ? 5 : \
+             ((x) < 128 ? 6 : 7)))))))
+
+/*! Calculate a compile-time log2 for a uint16_t */
+#define UINT16_LOG2(x) \
+       ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
+
+/*! Calculate a compile-time log2 for a uint32_t */
+#define UINT32_LOG2(x) \
+       ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
+
+/*! Concatenate two different preprocessor tokens (allowing macros to expand) */
+#define PP_CAT(x,y)         PP_CAT__(x,y)
+#define PP_CAT__(x,y)       x ## y
+#define PP_CAT3(x,y,z)      PP_CAT(PP_CAT(x,y),z)
+#define PP_CAT4(x,y,z,w)    PP_CAT(PP_CAT3(x,y,z),w)
+#define PP_CAT5(x,y,z,w,j)  PP_CAT(PP_CAT4(x,y,z,w),j)
+
+/*! String-ize a token (allowing macros to expand) */
+#define PP_STRINGIZE(x)     PP_STRINGIZE__(x)
+#define PP_STRINGIZE__(x)   #x
+
+#if COMPILER_C99
+       /*! Count the number of arguments (up to 16) */
+       #define PP_COUNT(...) \
+               PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
+       #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \
+               count
+#endif
+
+/*! Issue a compilation error if the \a condition is false */
+#define STATIC_ASSERT(condition)  \
+       extern char CT_ASSERT___[(condition) ? 1 : -1]
+
+
+#if COMPILER_C99
+       /*!
+        * \def BIT_CHANGE(reg, (mask, value), ...)
+        *
+        * This macro allows for efficient and compact bit toggling in a hardware
+        * register. It is meant to replace hand-coded cruft which toggles bits
+        * in sequence.
+        *
+        * It is possible to specify an unlimited pair of (mask, value) parameters.
+        * For instance:
+        *
+        * \code
+        * void set_timer(bool start)
+        * {
+        *     BIT_CHANGE(REG_CTRL_TIMER,
+        *        (TIMER_MODE, MODE_COUNT),
+        *        (OVL_IRQ, 1),
+        *        (CMP_IRQ, 1),
+        *        (START, start)
+        *     );
+        * }
+        * \endcode
+        *
+        * The macro expansion will be roughly the following:
+        *
+        * \code
+        * REG_CTRL_TIMER = (REG_CTRL_TIMER & ~(TIMER_MODE|OVL_IRQ|CMP_IRQ|START)
+        *                  | (MODE_COUNT|OVL_IRQ|CMP_IRQ|(start ? START : 0));
+        * \endcode
+        *
+        * It is up to the compiler to produce the optimal code. We checked that GCC produces
+        * the best code in most cases. We preferred this expansion over the use of a block
+        * with a local variable because CodeWarrior 6.1 was not able to remove completely the
+        * allocation of the local from the stack.
+        *
+        * \note This macro is available only in C99 because it makes use of variadic macros.
+        * It would be possible to make up an implementation with a slightly different syntax
+        * for use with C90 compilers, through Boost Preprocessor.
+        *
+        */
+
+       /*!
+        * \def BIT_CHANGE_BV(reg, (bit, value), ...)
+        *
+        * Similar to BIT_CHANGE(), but get bits instead of masks (and applies BV() to convert
+        * them to masks).
+        *
+        */
+
+       #define BIT_EXTRACT_FLAG_0(bit, value)  bit
+       #define BIT_EXTRACT_FLAG_1(bit, value)  BV(bit)
+       #define BIT_EXTRACT_VALUE__(bit, value) value
+
+       #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
+               ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
+               /**/
+
+       #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \
+               (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
+               /**/
+
+       #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \
+               (macro(use_bv, 0, max, a0) | \
+               macro(use_bv, 1, max, a1) | \
+               macro(use_bv, 2, max, a2) | \
+               macro(use_bv, 3, max, a3) | \
+               macro(use_bv, 4, max, a4) | \
+               macro(use_bv, 5, max, a5) | \
+               macro(use_bv, 6, max, a6) | \
+               macro(use_bv, 7, max, a7) | \
+               macro(use_bv, 8, max, a8) | \
+               macro(use_bv, 9, max, a9) | \
+               macro(use_bv, 10, max, a10) | \
+               macro(use_bv, 11, max, a11) | \
+               macro(use_bv, 12, max, a12) | \
+               macro(use_bv, 13, max, a13) | \
+               macro(use_bv, 14, max, a14) | \
+               macro(use_bv, 15, max, a15)) \
+               /**/
+
+       #define BIT_ITER__(macro, use_bv, ...) \
+               BIT_ITER__2(macro, use_bv, PP_COUNT(__VA_ARGS__), __VA_ARGS__, (0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1)) \
+               /**/
+
+       #define BIT_MASKS__(use_bv, ...) \
+               BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
+               /**/
+
+       #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
+               BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
+               /**/
+
+       #define BIT_CHANGE__(reg, use_bv, ...) \
+               ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
+               /**/
+
+       #define BIT_CHANGE(reg, ...)        BIT_CHANGE__(reg, 0, __VA_ARGS__)
+       #define BIT_CHANGE_BV(reg, ...)     BIT_CHANGE__(reg, 1, __VA_ARGS__)
+
+#endif /* COMPILER_C99 */
+
+#endif /* MACROS_H */
+
index 3767ecfc54e4d4754e1659d7f017fec6a1bea854..ddfc2e37d8ea5eb226f55a0739c700e82775ef51 100755 (executable)
@@ -31,6 +31,9 @@
 
 /*
  * $Log$
+ * Revision 1.3  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.2  2004/08/04 15:52:54  rasky
  * Merge da SC: fixato namespace dell'include guard
  *
@@ -66,7 +69,8 @@
 #define MWARE_HASHTABLE_H
 
 #include <compiler.h>
-#include <kdebug.h>
+#include <macros.h>
+#include <drv/kdebug.h>
 
 /*! Enable/disable support to declare special hash tables which maintain a copy of
  *  the key internally instead of relying on the hook to extract it from the data.
index b5d9775079209df4c7a6565bdd598976c9d72e6b..2bac2b21373d0747ec851147a9d8230d7291a5ca 100755 (executable)
@@ -15,6 +15,9 @@
 
 /*
  * $Log$
+ * Revision 1.3  2004/08/14 19:37:57  rasky
+ * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
+ *
  * Revision 1.2  2004/08/04 15:54:18  rasky
  * Merge da SC: prima versione veramente funzionante
  *
@@ -31,6 +34,7 @@
 
 #include "heap.h"
 #include <string.h>           // memset()
+#include <macros.h>           // IS_POW2()
 #include <drv/kdebug.h>       // ASSERT()
 
 /* NOTE: struct size must be a 2's power! */
diff --git a/mware/pool.h b/mware/pool.h
new file mode 100755 (executable)
index 0000000..dd18018
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef MWARE_POOL_H
+#define MWARE_POOL_H
+
+#include <macros.h>
+#include <mware/list.h>
+
+#define EXTERN_POOL(name) \
+       extern List name
+
+#define DECLARE_POOL_WITH_STORAGE(name, type, num, storage) \
+       static type name##_items[num]; \
+       storage name; \
+       INLINE void name##_init(void (*init_func)(type*)) \
+       { \
+               int i; \
+               INITLIST(&name); \
+               for (i=0;i<countof(name##_items);++i) \
+               { \
+                       if (init_func) init_func(&name##_items[i]); \
+                       ADDTAIL(&name, (Node*)&name##_items[i]); \
+               } \
+       } \
+       INLINE void name##_init(void (*init_func)(type*)) \
+       /**/
+
+#define DECLARE_POOL(name, type, num) \
+       DECLARE_POOL_WITH_STORAGE(name, type, num, List)
+
+#define DECLARE_POOL_STATIC(name, type, num) \
+       DECLARE_POOL_WITH_STORAGE(name, type, num, static List)
+
+#define pool_init(name, init_func)     (*(name##_init))(init_func)
+#define pool_alloc(name)               REMHEAD(name)
+#define pool_free(name, elem)          ADDHEAD(name, (Node*)elem)
+#define pool_empty(name)               ISLISTEMPTY(name)
+
+#endif