freemyipod r431 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r430‎ | r431 | r432 >
Date:04:43, 16 January 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Dynamically allocate button handlers
Modified paths:
  • /emcore/trunk/button.c (modified) (history)
  • /emcore/trunk/button.h (modified) (history)

Diff [purge]

Index: emcore/trunk/button.c
@@ -26,71 +26,84 @@
2727 #include "thread.h"
2828
2929
30 -#ifndef BUTTON_MAX_HOOKS
31 -#define BUTTON_MAX_HOOKS 16
32 -#endif
33 -
34 -
35 -static struct button_hook_entry button_hooks[BUTTON_MAX_HOOKS] IBSS_ATTR;
 30+static struct button_hook_entry* head_button_hook IBSS_ATTR;
3631 static struct mutex button_mutex;
3732
3833
3934 void button_init()
4035 {
41 - memset(button_hooks, 0, sizeof(button_hooks));
 36+ head_button_hook = NULL;
4237 mutex_init(&button_mutex);
4338 }
4439
4540 int button_register_handler(void (*handler)(enum button_event, int which, int value))
4641 {
47 - int i;
 42+ struct button_hook_entry* hook;
 43+ hook = (struct button_hook_entry*)malloc(sizeof(struct button_hook_entry));
 44+ hook->owner = current_thread;
 45+ hook->handler = handler;
4846 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
49 - for (i = 0; i < BUTTON_MAX_HOOKS; i++)
50 - if (button_hooks[i].owner == NULL)
51 - {
52 - button_hooks[i].owner = current_thread;
53 - button_hooks[i].handler = handler;
54 - mutex_unlock(&button_mutex);
55 - return 0;
56 - }
 47+ hook->next = head_button_hook;
 48+ head_button_hook = hook;
5749 mutex_unlock(&button_mutex);
58 - return -1;
 50+ return 0;
5951 }
6052
6153 int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
6254 {
63 - int i;
 55+ struct button_hook_entry* h;
 56+ struct button_hook_entry* handle = NULL;
 57+ int result = 0;
6458 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
65 - for (i = 0; i < BUTTON_MAX_HOOKS; i++)
66 - if (button_hooks[i].handler == handler)
 59+ if (head_button_hook && head_button_hook->handler == handler)
 60+ {
 61+ handle = head_button_hook;
 62+ head_button_hook = head_button_hook->next;
 63+ }
 64+ else
 65+ {
 66+ for (h = head_button_hook; h && h->next->handler != handler; h = h->next);
 67+ if (h)
6768 {
68 - button_hooks[i].owner = NULL;
69 - button_hooks[i].handler = NULL;
70 - mutex_unlock(&button_mutex);
71 - return 0;
 69+ handle = h->next;
 70+ h->next = h->next->next;
7271 }
 72+ else result = -1;
 73+ }
7374 mutex_unlock(&button_mutex);
74 - return -1;
 75+ if (handle) free(handle);
 76+ return result;
7577 }
7678
7779 void button_send_event(enum button_event eventtype, int which, int value)
7880 {
7981 DEBUGF("Sending button event: %d, %02X, %02X", eventtype, which, value);
80 - int i;
81 - for (i = 0; i < BUTTON_MAX_HOOKS; i++)
82 - if (button_hooks[i].owner != NULL)
83 - button_hooks[i].handler(eventtype, which, value);
 82+ struct button_hook_entry* h;
 83+ mutex_lock(&button_mutex, TIMEOUT_BLOCK);
 84+ for (h = head_button_hook; h; h = h->next)
 85+ h->handler(eventtype, which, value);
 86+ mutex_unlock(&button_mutex);
8487 }
8588
8689 void button_unregister_all_of_thread(struct scheduler_thread* process)
8790 {
88 - int i;
 91+ struct button_hook_entry* h;
 92+ struct button_hook_entry* prev;
8993 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
90 - for (i = 0; i < BUTTON_MAX_HOOKS; i++)
91 - if (button_hooks[i].owner == process)
 94+ while (head_button_hook && head_button_hook->owner == process)
 95+ {
 96+ prev = head_button_hook;
 97+ head_button_hook = head_button_hook->next;
 98+ free(prev);
 99+ }
 100+ for (h = head_button_hook->next; h; h = h->next)
 101+ {
 102+ while (h && h->owner == process)
92103 {
93 - button_hooks[i].owner = NULL;
94 - button_hooks[i].handler = NULL;
 104+ prev->next = h->next;
 105+ free(h);
95106 }
 107+ prev = h;
 108+ }
96109 mutex_unlock(&button_mutex);
97110 }
Index: emcore/trunk/button.h
@@ -43,6 +43,7 @@
4444
4545 struct button_hook_entry
4646 {
 47+ struct button_hook_entry* next;
4748 struct scheduler_thread* owner;
4849 void (*handler)(enum button_event, int which, int value);
4950 };