ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

index.c
Go to the documentation of this file.
00001 /*
00002     index: frame index data structure and functions
00003 
00004     copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
00005     see COPYING and AUTHORS files in distribution or http://mpg123.org
00006     initially written by Thomas Orgis
00007 */
00008 
00009 #include "index.h"
00010 #include "debug.h"
00011 
00012 /* The next expected frame offset, one step ahead. */
00013 static off_t fi_next(struct frame_index *fi)
00014 {
00015     return (off_t)fi->fill*fi->step;
00016 }
00017 
00018 /* Shrink down the used index to the half.
00019    Be careful with size = 1 ... there's no shrinking possible there. */
00020 static void fi_shrink(struct frame_index *fi)
00021 {
00022     if(fi->fill < 2) return; /* Won't shrink below 1. */
00023     else
00024     { /* Double the step, half the fill. Should work as well for fill%2 = 1 */
00025         size_t c;
00026         debug2("shrink index with fill %lu and step %lu", (unsigned long)fi->fill, (unsigned long)fi->step);
00027         fi->step *= 2;
00028         fi->fill /= 2;
00029         /* Move the data down. */
00030         for(c = 0; c < fi->fill; ++c)
00031         fi->data[c] = fi->data[2*c];
00032     }
00033 
00034     fi->next = fi_next(fi);
00035 }
00036 
00037 void fi_init(struct frame_index *fi)
00038 {
00039     fi->data = NULL;
00040     fi->step = 1;
00041     fi->fill = 0;
00042     fi->size = 0;
00043     fi->grow_size = 0;
00044     fi->next = fi_next(fi);
00045 }
00046 
00047 void fi_exit(struct frame_index *fi)
00048 {
00049     debug2("fi_exit: %p and %lu", (void*)fi->data, (unsigned long)fi->size);
00050     if(fi->size && fi->data != NULL) free(fi->data);
00051 
00052     fi_init(fi); /* Be prepared for further fun, still. */
00053 }
00054 
00055 int fi_resize(struct frame_index *fi, size_t newsize)
00056 {
00057     off_t *newdata = NULL;
00058     if(newsize == fi->size) return 0;
00059 
00060     if(newsize > 0 && newsize < fi->size)
00061     { /* When we reduce buffer size a bit, shrink stuff. */
00062         while(fi->fill > newsize){ fi_shrink(fi); }
00063     }
00064 
00065     newdata = safe_realloc(fi->data, newsize*sizeof(off_t));
00066     if(newsize == 0 || newdata != NULL)
00067     {
00068         fi->data = newdata;
00069         fi->size = newsize;
00070         if(fi->fill > fi->size) fi->fill = fi->size;
00071 
00072         fi->next = fi_next(fi);
00073         debug2("new index of size %lu at %p", (unsigned long)fi->size, (void*)fi->data);
00074         return 0;
00075     }
00076     else
00077     {
00078         error("failed to resize index!");
00079         return -1;
00080     }
00081 }
00082 
00083 void fi_add(struct frame_index *fi, off_t pos)
00084 {
00085     debug3("wanting to add to fill %lu, step %lu, size %lu", (unsigned long)fi->fill, (unsigned long)fi->step, (unsigned long)fi->size);
00086     if(fi->fill == fi->size)
00087     { /* Index is full, we need to shrink... or grow. */
00088         /* Store the current frame number to check later if we still want it. */
00089         off_t framenum = fi->fill*fi->step;
00090         /* If we want not / cannot grow, we shrink. */  
00091         if( !(fi->grow_size && fi_resize(fi, fi->size+fi->grow_size)==0) )
00092         fi_shrink(fi);
00093 
00094         /* Now check if we still want to add this frame (could be that not, because of changed step). */
00095         if(fi->next != framenum) return;
00096     }
00097     /* When we are here, we want that frame. */
00098     if(fi->fill < fi->size) /* safeguard for size=1, or just generally */
00099     {
00100         debug1("adding to index at %p", (void*)(fi->data+fi->fill));
00101         fi->data[fi->fill] = pos;
00102         ++fi->fill;
00103         fi->next = fi_next(fi);
00104         debug3("added pos %li to index with fill %lu and step %lu", (long) pos, (unsigned long)fi->fill, (unsigned long)fi->step);
00105     }
00106 }
00107 
00108 int fi_set(struct frame_index *fi, off_t *offsets, off_t step, size_t fill)
00109 {
00110     if(fi_resize(fi, fill) == -1) return -1;
00111     fi->step = step;
00112     if(offsets != NULL)
00113     {
00114         memcpy(fi->data, offsets, fill*sizeof(off_t));
00115         fi->fill = fill;
00116     }
00117     else
00118     {
00119         /* allocation only, no entries in index yet */
00120         fi->fill = 0;
00121     }
00122     fi->next = fi_next(fi);
00123     debug3("set new index of fill %lu, size %lu at %p",
00124     (unsigned long)fi->fill, (unsigned long)fi->size, (void*)fi->data);
00125     return 0;
00126 }
00127 
00128 void fi_reset(struct frame_index *fi)
00129 {
00130     debug1("reset with size %"SIZE_P, (size_p)fi->size);
00131     fi->fill = 0;
00132     fi->step = 1;
00133     fi->next = fi_next(fi);
00134 }

Generated on Sat May 26 2012 04:22:22 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.