my_reg_alloc.c

00001 
00002 /*---------------------------------------------------------------------------
00003    
00004    File name    :   reg_alloc.c
00005    Author       :   N. Devillard
00006    Created on   :   February 2000
00007    Description  :   cube allocation tracking routines
00008 
00009  *--------------------------------------------------------------------------*/
00010 
00011 /*
00012     $Id: reg_alloc.c,v 1.5 2000/10/11 14:09:05 ndevilla Exp $
00013     $Author: ndevilla $
00014     $Date: 2000/10/11 14:09:05 $
00015     $Revision: 1.5 $
00016 */
00017 
00018 /*---------------------------------------------------------------------------
00019                                 Includes
00020  ---------------------------------------------------------------------------*/
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 #include "reg_alloc.h"
00026 
00027 /*---------------------------------------------------------------------------
00028                                 Defines
00029  ---------------------------------------------------------------------------*/
00030 
00031 /*
00032  * Undefine malloc and free to get the true functions from libc.
00033  * Allocations within this module should not be hindered in any way.
00034  */
00035 #undef malloc
00036 #undef free
00037 
00038 
00039 /*---------------------------------------------------------------------------
00040                                 New types
00041  ---------------------------------------------------------------------------*/
00042 
00043 /*
00044  * Strictly local to this module
00045  */
00046 typedef struct _REG_ALLOC_ {
00047 
00048     struct _REG_ALLOC_  *   next ;
00049     struct _REG_ALLOC_  *   prev ;
00050     struct _REG_ALLOC_  *   list ;
00051 
00052     void    *   data ;
00053 } reg_alloc ;
00054 
00055 
00056 
00057 
00058 /*---------------------------------------------------------------------------
00059                     Private variables and functions
00060  ---------------------------------------------------------------------------*/
00061 
00062 static reg_alloc *  reg_alloc_base = NULL ;
00063 
00064 static reg_alloc *  reg_alloc_new(void);
00065 static void         reg_alloc_free(reg_alloc ** t);
00066 static void         reg_alloc_dump(void);
00067 
00068 /*---------------------------------------------------------------------------
00069                             Function codes
00070  ---------------------------------------------------------------------------*/
00071 
00072 
00073 
00074 /*-------------------------------------------------------------------------*/
00084 /*--------------------------------------------------------------------------*/
00085 
00086 static reg_alloc * reg_alloc_new(void)
00087 {
00088     reg_alloc   *   t ;
00089 
00090     t = malloc(sizeof(reg_alloc));
00091     t->next = NULL ;
00092     t->prev = NULL ;
00093     t->list = NULL ;
00094     t->data = NULL ;
00095     return t ;
00096 }
00097 
00098 /*-------------------------------------------------------------------------*/
00108 /*--------------------------------------------------------------------------*/
00109 
00110 static void reg_alloc_free(reg_alloc ** t)
00111 {
00112     free(*t);
00113     *t = NULL ;
00114 }
00115 
00116 
00117 /*-------------------------------------------------------------------------*/
00126 /*--------------------------------------------------------------------------*/
00127 static void reg_alloc_dump(void)
00128 {
00129     reg_alloc   *   fp ;
00130     reg_alloc   *   ip ;
00131 
00132     if (reg_alloc_base == NULL) return ;
00133 
00134     fprintf(stderr, "*** reg_alloc_base dump:\n");
00135     fprintf(stderr, "\n\n");
00136 
00137     fp = reg_alloc_base ;
00138     while (fp!=NULL) {
00139         fprintf(stderr, "[fp:%p]\t", fp->data);
00140         ip = fp->list ;
00141         while (ip!=NULL) {
00142             fprintf(stderr, "[%p]\t", ip->data);
00143             ip = ip->next ;
00144         }
00145         fprintf(stderr, "\n");
00146         fp = fp->next ;
00147     }
00148     fprintf(stderr, "\n\n");
00149     return ;
00150 }
00151 
00152 
00153 
00154 /*-------------------------------------------------------------------------*/
00168 /*--------------------------------------------------------------------------*/
00169 void reg_alloc_add(reg_alloc_entry * r)
00170 {
00171     reg_alloc   *   fp ;
00172     reg_alloc   *   ip ;
00173 
00174     if (r==NULL) return ;
00175 
00176     if (reg_alloc_base == NULL) {
00177         /* Add a new 'file' entry */
00178         reg_alloc_base = reg_alloc_new();
00179         reg_alloc_base->data = r->file_ref ;
00180         /* In this entry, add a new 'image' entry */
00181         reg_alloc_base->list = reg_alloc_new() ;
00182         reg_alloc_base->list->prev = reg_alloc_base ;
00183         reg_alloc_base->list->data = r->image_ref ;
00184         return ;
00185     }
00186 
00187     /* Try to find a file entry matching the one given */
00188 
00189     fp = reg_alloc_base ;
00190     while (fp->next!=NULL && fp->data!=r->file_ref) fp = fp->next ;
00191 
00192     if (fp->data == r->file_ref) {
00193         /* Matching file entry was found: add new image entry */
00194         ip = fp->list ;
00195         while (ip->next != NULL) ip=ip->next ;
00196         ip->next = reg_alloc_new() ;
00197         ip->next->prev = ip ;
00198         ip->next->data = r->image_ref ;
00199     } else {
00200         /* No matching file entry: create a new one */
00201         fp->next = reg_alloc_new();
00202         fp->next->data = r->file_ref ;
00203         fp->next->prev = fp ;
00204         /* In this entry, add a new 'image' entry */
00205         ip = fp->next->list = reg_alloc_new();
00206         ip->prev = fp->next ;
00207         ip->data = r->image_ref ;
00208     }
00209     return ;
00210 }
00211 
00212 
00213 /*-------------------------------------------------------------------------*/
00225 /*--------------------------------------------------------------------------*/
00226 
00227 int reg_alloc_remove(reg_alloc_entry * r)
00228 {
00229     int             count;
00230     reg_alloc   *   fp ;
00231     reg_alloc   *   ip ;
00232     reg_alloc   *   prev,
00233                 *   next ;
00234 
00235     if (reg_alloc_base == NULL) return -1 ;
00236     /* Try to find a file entry matching the one given */
00237     fp = reg_alloc_base ;
00238     while (fp->next!=NULL && fp->data!=r->file_ref) fp=fp->next ;
00239 
00240     if (fp->data!=r->file_ref) {
00241         fprintf(stderr, "internal error: fileref [%p] not found",
00242                 r->file_ref);
00243         reg_alloc_dump();
00244         return -1 ;
00245     }
00246 
00247     /* Try to find an image entry matching the one given */
00248     ip = fp->list ;
00249     while (ip->next!=NULL && ip->data!=r->image_ref) ip=ip->next;
00250     if (ip->data != r->image_ref) {
00251         fprintf(stderr, "internal error: imageref [%p] not found",
00252                 r->image_ref);
00253         reg_alloc_dump();
00254         return -1 ;
00255     }
00256 
00257     /* Entry was found: remove it now */
00258     if (ip->prev->list == ip) {
00259         ip->prev->list = ip->next ;
00260     } else {
00261         ip->prev->next = ip->next ;
00262     }
00263     if (ip->next!=NULL) ip->next->prev = ip->prev ;
00264     reg_alloc_free(&ip) ;
00265 
00266     /* Count how many image references are left */
00267     if (fp->list == NULL) {
00268         /* No more image references: unhook file reference, too */
00269         prev = fp->prev ;
00270         next = fp->next ;
00271         reg_alloc_free(&fp);
00272 
00273         if (prev != NULL) {
00274             prev->next = next ;
00275         } else {
00276             reg_alloc_base = next ;
00277         }
00278         if (next != NULL) {
00279             next->prev = prev ;
00280         }
00281         count = 0 ;
00282     } else {
00283         ip = fp->list ;
00284         count = 1 ;
00285         while (ip->next!=NULL) {
00286             ip = ip->next ;
00287             count ++ ;
00288         }
00289     }
00290     return count ;
00291 }
00292 
00293 
00294 /*-------------------------------------------------------------------------*/
00304 /*--------------------------------------------------------------------------*/
00305 void reg_alloc_status(void)
00306 {
00307     if (reg_alloc_base!=NULL) {
00308         fprintf(stderr, "\n\n");
00309         fprintf(stderr, "*** mmapping status\n");
00310         fprintf(stderr, "*** some file pointers are still allocated\n"); 
00311         fprintf(stderr, "\n\n");
00312         reg_alloc_dump() ;
00313     }
00314     return ;
00315 }
00316 
00317 

Generated on Wed Oct 26 13:08:53 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001