~ubuntu-branches/ubuntu/hardy/linux-restricted-modules-envy-2.6.24/hardy-proposed

« back to all changes in this revision

Viewing changes to fritz/fcusb/src/tools.c

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Milone (tseliot)
  • Date: 2008-05-05 11:01:43 UTC
  • Revision ID: james.westby@ubuntu.com-20080505110143-gi9niqn3vkf11h6c
Tags: 2.6.24.500-500.26
* Removed ~envy in the package version
* nvidia-glx{-new, -legacy}-envy all provide nvidia-glx-dev
* Removed version from Conflicts of all the packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * tools.c
 
3
 * Copyright (C) 2002, AVM GmbH. All rights reserved.
 
4
 * 
 
5
 * This Software is  free software. You can redistribute and/or
 
6
 * modify such free software under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 * 
 
10
 * The free software is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this Software; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, or see
 
18
 * http://www.opensource.org/licenses/lgpl-license.html
 
19
 * 
 
20
 * Contact: AVM GmbH, Alt-Moabit 95, 10559 Berlin, Germany, email: info@avm.de
 
21
 */
 
22
 
 
23
#include <linux/slab.h>
 
24
#include <linux/string.h>
 
25
#include <linux/vmalloc.h>
 
26
#include <linux/kernel.h>
 
27
#include "defs.h"
 
28
#include "libdefs.h"
 
29
#include "driver.h"
 
30
#include "lib.h"
 
31
#include "lock.h"
 
32
#include "tools.h"
 
33
 
 
34
/*---------------------------------------------------------------------------*\
 
35
\*---------------------------------------------------------------------------*/
 
36
struct __lock {
 
37
 
 
38
        unsigned long           flags;
 
39
        spinlock_t              lock;
 
40
} ; 
 
41
 
 
42
/*---------------------------------------------------------------------------*\
 
43
\*---------------------------------------------------------------------------*/
 
44
#if defined (TOOLS_PTR_QUEUE)
 
45
struct __q {
 
46
 
 
47
        unsigned        head, tail, mask;
 
48
        unsigned        size, free;
 
49
        unsigned        bnum, blen;
 
50
        void **         item;
 
51
        char *          bptr;
 
52
} ;
 
53
#endif
 
54
 
 
55
/*---------------------------------------------------------------------------*\
 
56
\*---------------------------------------------------------------------------*/
 
57
typedef struct __hdr {
 
58
 
 
59
        unsigned        type;
 
60
#if !defined (NDEBUG)
 
61
#if defined (TOOLS_FREE_CATCH)
 
62
        unsigned        nfree;
 
63
#endif
 
64
        unsigned        size;
 
65
        unsigned        tag;
 
66
#endif
 
67
} header_t;
 
68
 
 
69
/*---------------------------------------------------------------------------*\
 
70
\*---------------------------------------------------------------------------*/
 
71
#define KMALLOC_LIMIT   131072
 
72
 
 
73
#define TYPE_NONE       '?'
 
74
#define TYPE_KMALLOCED  'k'
 
75
#define TYPE_VMALLOCED  'v'
 
76
#define TYPE_SMALLOCED  's'
 
77
 
 
78
#define ALLOC_NORMAL    0
 
79
#define ALLOC_SPECIAL   1
 
80
 
 
81
#if !defined (NDEBUG)
 
82
#define PATCH(n)        sizeof(header_t)+sizeof(unsigned)+((n)?(n):1)
 
83
#else
 
84
#define PATCH(n)        sizeof(header_t)+((n)?(n):1)
 
85
#endif
 
86
 
 
87
static void *           halloc (unsigned, int, int);
 
88
 
 
89
#define __ALLOC(s,p)    halloc((s),(p),ALLOC_NORMAL)
 
90
 
 
91
/*---------------------------------------------------------------------------*\
 
92
\*---------------------------------------------------------------------------*/
 
93
#if defined (TOOLS_NOMEM_HANDLER)
 
94
static nomem_handler_t  handler         = NULL;
 
95
#endif
 
96
 
 
97
#if defined (TOOLS_SUB_ALLOC)
 
98
static void *           lib_heap_base   = NULL;
 
99
static unsigned         lib_heap_size   = 0;
 
100
#endif
 
101
 
 
102
/*---------------------------------------------------------------------------*\
 
103
\*---------------------------------------------------------------------------*/
 
104
#if !defined (NDEBUG)
 
105
#include <asm/atomic.h>
 
106
#include <linux/spinlock.h>
 
107
 
 
108
#define FENCE_TAG       0xDEADBEEF
 
109
#define FENCE1_OK(h,m)  ((h)->tag==FENCE_TAG)
 
110
#define FENCE2_OK(h,m)  (*(unsigned *)(((char *) m)+(h)->size)==FENCE_TAG)
 
111
 
 
112
static unsigned         alloc_count     = 0;
 
113
static spinlock_t       track_lock      = SPIN_LOCK_UNLOCKED;
 
114
 
 
115
#if !defined (NDEBUG) && defined (LOG_TIMER)
 
116
static struct timeval   zero_time;
 
117
#endif
 
118
#endif
 
119
 
 
120
/*---------------------------------------------------------------------------*\
 
121
\*---------------------------------------------------------------------------*/
 
122
#if !defined (NDEBUG)
 
123
static inline void track_alloc (header_t * hdr) {
 
124
        unsigned long   flags;
 
125
 
 
126
#if defined (LOG_ALLOC)
 
127
        LOG("ALLOC: %p, %u, type %c\n", hdr + 1, hdr->size, hdr->type);
 
128
#endif
 
129
        spin_lock_irqsave (&track_lock, flags);
 
130
        alloc_count += hdr->size;
 
131
        /* <<< */
 
132
        spin_unlock_irqrestore (&track_lock, flags);
 
133
} /* track_alloc */
 
134
#endif
 
135
 
 
136
/*---------------------------------------------------------------------------*\
 
137
\*---------------------------------------------------------------------------*/
 
138
#if !defined (NDEBUG)
 
139
static inline void track_free (header_t * hdr) {
 
140
        unsigned long   flags;
 
141
 
 
142
#if defined (LOG_ALLOC)
 
143
        LOG("FREE: %p, %u, type %c\n", hdr + 1, hdr->size, hdr->type);
 
144
#endif
 
145
        spin_lock_irqsave (&track_lock, flags);
 
146
        info (alloc_count >= hdr->size);
 
147
        alloc_count -= hdr->size;
 
148
        /* <<< */
 
149
        spin_unlock_irqrestore (&track_lock, flags);
 
150
} /* track_free */
 
151
#endif
 
152
 
 
153
/*---------------------------------------------------------------------------*\
 
154
\*---------------------------------------------------------------------------*/
 
155
#if defined (TOOLS_SUB_ALLOC)
 
156
unsigned libheap_init (unsigned heap_size) {
 
157
        void *  heap_base = NULL;
 
158
 
 
159
        assert (lib_heap_base == NULL);
 
160
        assert (heap_size > MIN_LIB_HEAP_SIZE);
 
161
        do {
 
162
                heap_base = halloc (heap_size, GFP_ATOMIC, ALLOC_SPECIAL);
 
163
                info (heap_base != NULL);
 
164
                if (NULL != heap_base) {
 
165
                        lib_heap_base = heap_base;
 
166
                        lib_heap_size = heap_size;
 
167
                        (*capi_lib->lib_heap_init) (heap_base, heap_size);
 
168
                        return heap_size;
 
169
                }
 
170
                heap_size /= 2;
 
171
        } while (heap_size > 0);
 
172
        return 0;
 
173
} /* libheap_init */
 
174
#endif
 
175
 
 
176
/*---------------------------------------------------------------------------*\
 
177
\*---------------------------------------------------------------------------*/
 
178
#if defined (TOOLS_SUB_ALLOC)
 
179
void libheap_exit (void) {
 
180
 
 
181
        assert (lib_heap_base != NULL);
 
182
        (*capi_lib->lib_heap_exit) (lib_heap_base);
 
183
        hfree (lib_heap_base);
 
184
        lib_heap_base = NULL;
 
185
        lib_heap_size = 0;
 
186
} /* libheap_exit */
 
187
#endif
 
188
 
 
189
/*---------------------------------------------------------------------------*\
 
190
\*---------------------------------------------------------------------------*/
 
191
#if defined (TOOLS_NOMEM_HANDLER)
 
192
nomem_handler_t hset_handler (nomem_handler_t hndf) {
 
193
        nomem_handler_t oldf = handler;
 
194
        
 
195
        handler = hndf;
 
196
        return oldf;
 
197
} /* hset_handler */
 
198
#endif
 
199
 
 
200
/*---------------------------------------------------------------------------*\
 
201
\*---------------------------------------------------------------------------*/
 
202
#if !defined (NDEBUG)
 
203
unsigned hallocated (void) {
 
204
    
 
205
        return alloc_count;
 
206
} /* hallocated */
 
207
#endif
 
208
 
 
209
/*---------------------------------------------------------------------------*\
 
210
\*---------------------------------------------------------------------------*/
 
211
#if !defined (NDEBUG)
 
212
int hvalid (void * mem) {
 
213
        header_t *      hdr;
 
214
        int             flag = TRUE;
 
215
 
 
216
        if (mem != NULL) {
 
217
                hdr  = ((header_t *) mem) - 1;
 
218
                flag = FENCE1_OK(hdr, mem) && FENCE2_OK(hdr, mem);
 
219
        } 
 
220
        return flag;
 
221
} /* hvalid */
 
222
#endif
 
223
 
 
224
/*---------------------------------------------------------------------------*\
 
225
\*---------------------------------------------------------------------------*/
 
226
void * hmalloc (unsigned size) {
 
227
 
 
228
        return __ALLOC(size, GFP_ATOMIC);
 
229
} /* hmalloc */
 
230
 
 
231
/*---------------------------------------------------------------------------*\
 
232
\*---------------------------------------------------------------------------*/
 
233
void * hcalloc (unsigned size) {
 
234
        void * mem;
 
235
 
 
236
        mem = __ALLOC(size, GFP_ATOMIC);
 
237
        if ((mem != NULL) && (size != 0)) {
 
238
                lib_memset (mem, 0, size);
 
239
        }
 
240
        return mem;
 
241
} /* hcalloc */
 
242
 
 
243
/*---------------------------------------------------------------------------*\
 
244
\*---------------------------------------------------------------------------*/
 
245
void * hmalloc_kernel (unsigned size) {
 
246
 
 
247
        return __ALLOC(size, GFP_KERNEL);
 
248
} /* hmalloc */
 
249
 
 
250
/*---------------------------------------------------------------------------*\
 
251
\*---------------------------------------------------------------------------*/
 
252
void * hcalloc_kernel (unsigned size) {
 
253
        void * mem;
 
254
 
 
255
        mem = __ALLOC(size, GFP_KERNEL);
 
256
        if ((mem != NULL) && (size != 0)) {
 
257
                lib_memset (mem, 0, size);
 
258
        }
 
259
        return mem;
 
260
} /* hcalloc */
 
261
 
 
262
/*---------------------------------------------------------------------------*\
 
263
\*---------------------------------------------------------------------------*/
 
264
static void * halloc (unsigned size, int prio, int mode) {
 
265
        unsigned        n, type;
 
266
        void *          mem;
 
267
        header_t *      hdr;
 
268
 
 
269
        /* Allocate */
 
270
        n = PATCH(size);
 
271
        if (n <= KMALLOC_LIMIT) {
 
272
                hdr  = kmalloc (n, prio);
 
273
                type = TYPE_KMALLOCED;
 
274
        } else {
 
275
#if defined (TOOLS_SUB_ALLOC)
 
276
                if (ALLOC_NORMAL == mode) {
 
277
                        info (lib_heap_base != NULL);
 
278
                        assert (capi_lib->lib_heap_alloc != NULL);
 
279
                        hdr  = (* capi_lib->lib_heap_alloc) (lib_heap_base, n);
 
280
                        type = TYPE_SMALLOCED;
 
281
                } else {
 
282
                        assert (ALLOC_SPECIAL == mode);
 
283
                        hdr  = vmalloc (n);
 
284
                        type = TYPE_VMALLOCED;
 
285
                }
 
286
#else
 
287
                hdr  = vmalloc (n);
 
288
                type = TYPE_VMALLOCED;
 
289
#endif
 
290
        }
 
291
 
 
292
        /* Accounting & debugging */
 
293
        info (hdr != NULL);
 
294
        if (NULL == hdr) {
 
295
                LOG(
 
296
                        "Memory request (%u/%u bytes) failed.\n", 
 
297
                        size, 
 
298
                        PATCH(size)
 
299
                );
 
300
                mem = NULL;
 
301
#if defined (TOOLS_NOMEM_HANDLER)
 
302
                if (handler != NULL) {
 
303
                        (*handler) (size);
 
304
                }
 
305
#endif
 
306
        } else {
 
307
                mem = (void *) (hdr + 1);
 
308
                hdr->type = type;
 
309
#if !defined (NDEBUG)
 
310
                hdr->size = size ? size : 1;
 
311
                hdr->tag = FENCE_TAG;
 
312
#if defined (TOOLS_FREE_CATCH)
 
313
                hdr->nfree = 0;
 
314
#endif
 
315
                * (unsigned *) (((char *) mem) + size) = FENCE_TAG;
 
316
                track_alloc (hdr);
 
317
#endif
 
318
        }
 
319
        return mem;
 
320
} /* halloc */
 
321
 
 
322
/*---------------------------------------------------------------------------*\
 
323
\*---------------------------------------------------------------------------*/
 
324
void hfree (void * mem) {
 
325
        header_t *      hdr;
 
326
 
 
327
        info (mem != NULL);
 
328
        if (mem != NULL) {
 
329
 
 
330
                /* Accounting & checking */
 
331
                hdr = ((header_t *) mem) - 1;
 
332
#if !defined (NDEBUG)
 
333
                if (!(FENCE1_OK(hdr, mem) && FENCE2_OK(hdr, mem))) {
 
334
                        LOG(
 
335
                                "FENCE VIOLATED (%u bytes @ %p)!\n", 
 
336
                                hdr->size, 
 
337
                                mem
 
338
                        );
 
339
                }
 
340
                track_free (hdr);
 
341
#endif
 
342
 
 
343
                /* Release */
 
344
#if !defined (TOOLS_FREE_CATCH)
 
345
                switch (hdr->type) {
 
346
 
 
347
                default:
 
348
                        assert (0);
 
349
                        break;
 
350
                case TYPE_KMALLOCED:
 
351
                        kfree (hdr);
 
352
                        break;
 
353
                case TYPE_VMALLOCED:
 
354
                        vfree (hdr);
 
355
                        break;
 
356
#if defined (TOOLS_SUB_ALLOC)
 
357
                case TYPE_SMALLOCED:
 
358
                        assert (capi_lib->lib_heap_free != NULL);
 
359
                        info (lib_heap_base != NULL);
 
360
                        (* capi_lib->lib_heap_free) (lib_heap_base, hdr);
 
361
                        break;
 
362
#endif
 
363
                }
 
364
#else
 
365
                if (hdr->nfree != 0) {
 
366
                        ERROR(
 
367
                                "ALREADY FREED (%u bytes @ %p)!\n",
 
368
                                hdr->size,
 
369
                                mem
 
370
                        );
 
371
                }
 
372
                hdr->nfree++;
 
373
                /* Memory leak! */
 
374
#endif
 
375
        }
 
376
} /* hfree */
 
377
 
 
378
/*---------------------------------------------------------------------------*\
 
379
\*-T-------------------------------------------------------------------------*/
 
380
 
 
381
#if !defined (NDEBUG) && defined (LOG_TIMER)
 
382
 
 
383
#include <linux/time.h>
 
384
 
 
385
void setup_timer (dbg_timer * t, long dsec, long dusec) {
 
386
        
 
387
        assert (t != NULL);
 
388
        lib_memset (&t->t, 0, sizeof (t->t));
 
389
        t->d.tv_sec  = dsec;
 
390
        t->d.tv_usec = dusec;
 
391
} /* setup_timer */
 
392
 
 
393
/*---------------------------------------------------------------------------*\
 
394
\*---------------------------------------------------------------------------*/
 
395
int check_timer (dbg_timer * t) {
 
396
        int             res = 1;
 
397
        struct timeval  now;
 
398
        struct timeval  delta;
 
399
        
 
400
        assert (t != NULL);
 
401
        do_gettimeofday (&now);
 
402
        timeval_less (now, zero_time, &delta);
 
403
        now = delta;
 
404
        timeval_less (now, t->t, &delta);
 
405
        if ((delta.tv_sec > t->d.tv_sec) 
 
406
        || ((delta.tv_sec == t->d.tv_sec) && (delta.tv_usec > t->d.tv_usec))
 
407
        ) {
 
408
                note (
 
409
                        "Timer '%s' exceeded: %ld s, %ld �s\n", 
 
410
                        t->name,
 
411
                        delta.tv_sec,
 
412
                        delta.tv_usec
 
413
                );
 
414
                res = 0;
 
415
        } 
 
416
        return res;
 
417
} /* check_timer */
 
418
 
 
419
/*---------------------------------------------------------------------------*\
 
420
\*---------------------------------------------------------------------------*/
 
421
int check_timer_cb (dbg_timer * t, void (* callback) (dbg_timer *, struct timeval *)) {
 
422
        int             res = 1;
 
423
        struct timeval  now;
 
424
        struct timeval  delta;
 
425
        
 
426
        assert (t != NULL);
 
427
        do_gettimeofday (&now);
 
428
        timeval_less (now, zero_time, &delta);
 
429
        now = delta;
 
430
        timeval_less (now, t->t, &delta);
 
431
        if ((delta.tv_sec > t->d.tv_sec) 
 
432
        || ((delta.tv_sec == t->d.tv_sec) && (delta.tv_usec > t->d.tv_usec))
 
433
        ) {
 
434
                if (callback != NULL) {
 
435
                        (*callback) (t, &delta);
 
436
                }
 
437
                res = 0;
 
438
        } 
 
439
        return res;
 
440
} /* check_timer_cb */
 
441
 
 
442
/*---------------------------------------------------------------------------*\
 
443
\*---------------------------------------------------------------------------*/
 
444
void touch_timer (dbg_timer * t) {
 
445
        struct timeval  temp, delta;
 
446
        
 
447
        assert (t != NULL);
 
448
        do_gettimeofday (&temp);
 
449
        timeval_less (temp, zero_time, &delta);
 
450
        t->t = delta;
 
451
} /* touch_timer */
 
452
 
 
453
/*---------------------------------------------------------------------------*\
 
454
\*---------------------------------------------------------------------------*/
 
455
void start_watch (dbg_timer * w) {
 
456
        struct timeval  temp, delta;
 
457
        
 
458
        assert (w != NULL);
 
459
        do_gettimeofday (&temp);
 
460
        timeval_less (temp, zero_time, &delta);
 
461
        w->t = delta;
 
462
} /* start_watch */
 
463
 
 
464
/*---------------------------------------------------------------------------*\
 
465
\*---------------------------------------------------------------------------*/
 
466
void stop_watch (dbg_timer * w) {
 
467
        struct timeval  temp, delta;
 
468
        
 
469
        assert (w != NULL);
 
470
        do_gettimeofday (&temp);
 
471
        timeval_less (temp, zero_time, &delta);
 
472
        temp = delta;
 
473
        timeval_less (temp, w->t, &delta);
 
474
        w->t = delta;
 
475
} /* stop_watch */
 
476
 
 
477
/*---------------------------------------------------------------------------*\
 
478
\*---------------------------------------------------------------------------*/
 
479
int timers_start (void) {
 
480
 
 
481
        do_gettimeofday (&zero_time);
 
482
        return 1;
 
483
} /* timers_start */
 
484
     
 
485
/*---------------------------------------------------------------------------*\
 
486
\*---------------------------------------------------------------------------*/
 
487
void timers_stop (void) {
 
488
 
 
489
} /* timers_stop */
 
490
 
 
491
#endif /* !NDEBUG && LOG_TIMER */
 
492
 
 
493
/*---------------------------------------------------------------------------*\
 
494
\*-M-------------------------------------------------------------------------*/
 
495
void vlprintf (const char * level, const char * fmt, va_list args) {
 
496
        static char line[1024];
 
497
 
 
498
        vsnprintf (line, sizeof (line), fmt, args);
 
499
        printk ("%s%s: %s", level, TARGET, line); 
 
500
} /* vlprintf */
 
501
 
 
502
/*---------------------------------------------------------------------------*\
 
503
\*---------------------------------------------------------------------------*/
 
504
void lprintf (const char * level, const char * fmt, ...) {
 
505
        va_list args;
 
506
 
 
507
        va_start (args, fmt);
 
508
        vlprintf (level, fmt, args);
 
509
        va_end (args);
 
510
} /* lprintf */
 
511
 
 
512
/*---------------------------------------------------------------------------*\
 
513
\*---------------------------------------------------------------------------*/
 
514
void message (const char * fmt, ...) {
 
515
        va_list args;
 
516
 
 
517
        va_start (args, fmt);
 
518
        vlprintf (KERN_INFO, fmt, args);
 
519
        va_end (args);
 
520
} /* message */
 
521
 
 
522
/*---------------------------------------------------------------------------*\
 
523
\*-L-------------------------------------------------------------------------*/
 
524
int lock_init (lock_t * plock) {
 
525
        lock_t  tmp;
 
526
 
 
527
        assert (plock != NULL);
 
528
        if (NULL == (tmp = (lock_t) hcalloc (sizeof (struct __lock)))) {
 
529
                ERROR("Could not allocate lock structure!!!\n");
 
530
                return 0;
 
531
        }
 
532
        tmp->lock = SPIN_LOCK_UNLOCKED;
 
533
        *plock = tmp;
 
534
        return 1;
 
535
} /* lock_init */
 
536
        
 
537
/*---------------------------------------------------------------------------*\
 
538
\*---------------------------------------------------------------------------*/
 
539
void lock_exit (lock_t * plock) {
 
540
 
 
541
        assert (plock != NULL);
 
542
        assert (*plock != NULL);
 
543
        assert (!spin_is_locked (&(*plock)->lock));
 
544
        hfree (*plock);
 
545
        *plock = NULL;
 
546
} /* lock_exit */
 
547
 
 
548
/*---------------------------------------------------------------------------*\
 
549
\*---------------------------------------------------------------------------*/
 
550
void lock (lock_t lp) {
 
551
        unsigned long   local_flags;
 
552
 
 
553
        assert (lp != NULL);
 
554
        spin_lock_irqsave (&lp->lock, local_flags);
 
555
        lp->flags = local_flags;
 
556
} /* lock */
 
557
 
 
558
/*---------------------------------------------------------------------------*\
 
559
\*---------------------------------------------------------------------------*/
 
560
void unlock (lock_t lp) {
 
561
        
 
562
        assert (lp != NULL);
 
563
        spin_unlock_irqrestore (&lp->lock, lp->flags);
 
564
} /* unlock */
 
565
 
 
566
/*---------------------------------------------------------------------------*\
 
567
\*-Q-------------------------------------------------------------------------*/
 
568
#if defined (TOOLS_PTR_QUEUE)
 
569
ptr_queue_p q_make (unsigned max) {
 
570
        ptr_queue_p     qp;
 
571
        unsigned        mask = 1;
 
572
        
 
573
        if (NULL == (qp = (ptr_queue_p) hmalloc (sizeof (ptr_queue_t)))) {
 
574
                return NULL;
 
575
        }
 
576
        if (NULL == (qp->item = (void **) hmalloc (max * sizeof (void *)))) {
 
577
                hfree (qp);
 
578
                return NULL;
 
579
        }
 
580
        qp->bptr = NULL;
 
581
        while (mask < max) {
 
582
                mask <<= 1;
 
583
        }
 
584
        assert (mask == max);
 
585
        --mask;
 
586
        qp->head = 0;
 
587
        qp->tail = 0;
 
588
        qp->mask = mask;
 
589
        qp->size = max;
 
590
        qp->free = max;
 
591
        return qp;
 
592
} /* q_make */
 
593
 
 
594
/*---------------------------------------------------------------------------*\
 
595
\*---------------------------------------------------------------------------*/
 
596
void q_remove (ptr_queue_p * qpp) {
 
597
        ptr_queue_p     qp;
 
598
        
 
599
        assert (qpp != NULL);
 
600
        qp = *qpp;
 
601
        assert (qp != NULL);
 
602
        assert (qp->item != NULL);
 
603
        if (qp->bptr != NULL) {
 
604
                hfree (qp->bptr);
 
605
        }
 
606
        hfree (qp->item);
 
607
        hfree (qp);
 
608
        *qpp = NULL;
 
609
} /* q_remove */
 
610
 
 
611
/*---------------------------------------------------------------------------*\
 
612
\*---------------------------------------------------------------------------*/
 
613
void q_reset (ptr_queue_p qp) {
 
614
 
 
615
        assert (qp != NULL);
 
616
        qp->head = qp->tail = 0;
 
617
        qp->free = qp->size;
 
618
} /* q_reset */
 
619
 
 
620
/*---------------------------------------------------------------------------*\
 
621
\*---------------------------------------------------------------------------*/
 
622
int q_attach_mem (ptr_queue_p qp, unsigned n, unsigned len) {
 
623
        void *          buf;
 
624
 
 
625
        assert (qp != NULL);
 
626
        assert (qp->bptr == 0);
 
627
        assert ((n * len) != 0);
 
628
        if (NULL == (buf = hmalloc (n * len))) {
 
629
                return FALSE;
 
630
        } 
 
631
        qp->bnum = n;
 
632
        qp->blen = len;
 
633
        qp->bptr = buf;
 
634
        return TRUE;
 
635
} /* q_attach_mem */
 
636
 
 
637
/*---------------------------------------------------------------------------*\
 
638
\*---------------------------------------------------------------------------*/
 
639
int q_enqueue (ptr_queue_p qp, void * p) {
 
640
 
 
641
        assert (qp != NULL);
 
642
        if (qp->free == 0) {
 
643
                return FALSE;
 
644
        }
 
645
        assert (qp->head < qp->size);
 
646
        qp->item[qp->head++] = p;
 
647
        qp->head &= qp->mask;
 
648
        assert (qp->head < qp->size);
 
649
        qp->free--;
 
650
        return TRUE;
 
651
} /* q_enqueue */
 
652
 
 
653
/*---------------------------------------------------------------------------*\
 
654
\*---------------------------------------------------------------------------*/
 
655
int q_enqueue_mem (ptr_queue_p qp, void * m, unsigned len) {
 
656
        unsigned        ix;
 
657
        char *          mp;
 
658
        
 
659
        assert (qp != NULL);
 
660
        assert (qp->bptr != NULL);
 
661
        assert (qp->blen >= len);
 
662
        if (qp->free == 0) {
 
663
                return FALSE;
 
664
        }
 
665
        assert (qp->head < qp->size);
 
666
        ix = qp->head++;
 
667
        qp->head &= qp->mask;
 
668
        qp->item[ix] = mp = &qp->bptr[ix * len];
 
669
        assert (mp != NULL);
 
670
        lib_memcpy (mp, m, len);
 
671
        assert (qp->head < qp->size);
 
672
        qp->free--;
 
673
        return TRUE;
 
674
} /* q_enqueue_mem */
 
675
 
 
676
/*---------------------------------------------------------------------------*\
 
677
\*---------------------------------------------------------------------------*/
 
678
int q_dequeue (ptr_queue_p qp, void ** pp) {
 
679
 
 
680
        assert (qp != NULL);
 
681
        if (qp->free == qp->size) {
 
682
                return FALSE;
 
683
        }
 
684
        assert (qp->tail < qp->size);
 
685
        assert (pp != NULL);
 
686
        *pp = qp->item[qp->tail++];
 
687
        qp->tail &= qp->mask;
 
688
        assert (qp->tail < qp->size);
 
689
        qp->free++;
 
690
        return TRUE;
 
691
} /* q_dequeue */
 
692
 
 
693
/*---------------------------------------------------------------------------*\
 
694
\*---------------------------------------------------------------------------*/
 
695
int q_peek (ptr_queue_p qp, void ** pp) {
 
696
 
 
697
        assert (qp != NULL);
 
698
        if (qp->free == qp->size) {
 
699
                return FALSE;
 
700
        }
 
701
        assert (qp->tail < qp->size);
 
702
        assert (pp != NULL);
 
703
        *pp = qp->item[qp->tail];
 
704
        return TRUE;
 
705
} /* q_peek */
 
706
 
 
707
/*---------------------------------------------------------------------------*\
 
708
\*---------------------------------------------------------------------------*/
 
709
unsigned q_get_count (ptr_queue_p qp) {
 
710
 
 
711
        assert (qp != NULL);
 
712
        return qp->size - qp->free;
 
713
} /* q_get_count */
 
714
#endif
 
715
 
 
716
/*---------------------------------------------------------------------------*\
 
717
\*---------------------------------------------------------------------------*/
 
718
#if defined (TOOLS_MEM_DUMP)
 
719
void memdump (
 
720
        const void *    mem,
 
721
        unsigned        len,
 
722
        unsigned        start,
 
723
        const char *    msg
 
724
) {
 
725
        unsigned        max, min, idx;
 
726
        unsigned char * data = (unsigned char *) mem;
 
727
        char            hex[50], chr[20];
 
728
 
 
729
        lprintf (KERN_INFO, "Memory dump %s:\n", msg);
 
730
        min = 0;
 
731
        while (min < len) {
 
732
                max = ((min + 16) > len ? len : min + 16);
 
733
                idx = 0;
 
734
                while ((min + idx) < max) {
 
735
                        snprintf (hex + 3 * idx, 4, "%02x ", *data);
 
736
                        snprintf (chr + idx, 2, "%c", ((' ' <= *data) &&
 
737
                                        (*data <= '~')) ? *data : '.');
 
738
                        ++idx;
 
739
                        ++data;
 
740
                }
 
741
                while (idx < 16) {
 
742
                        lib_strcpy (hex + 3 * idx++, "   ");
 
743
                }
 
744
                lprintf (KERN_INFO, "%08x: %s  %s\n", min + start, hex, chr);
 
745
                min = max;
 
746
        }
 
747
} /* memdump */
 
748
#endif
 
749
 
 
750
/*---------------------------------------------------------------------------*\
 
751
\*---------------------------------------------------------------------------*/
 
752
#ifndef NDEBUG
 
753
__attr void _OSassert (void * exp, void * file, unsigned line) {
 
754
 
 
755
        message ("assert (%s) in %s(%u)\n", exp, file, line);
 
756
} /* _OSassert */
 
757
 
 
758
__attr void _OSinfo (void * exp, void * file, unsigned line) {
 
759
 
 
760
        message ("info (%s) in %s(%u)\n", exp, file, line);
 
761
} /* _OSinfo */
 
762
#endif
 
763
 
 
764
/*---------------------------------------------------------------------------*\
 
765
\*---------------------------------------------------------------------------*/
 
766