"Fossies" - the Fresh Open Source Software Archive

Member "genius-1.0.24/vte/src/vterowdata.c" (16 Aug 2011, 3557 Bytes) of package /linux/misc/genius-1.0.24.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "vterowdata.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2002,2009 Red Hat, Inc.
    3  *
    4  * This is free software; you can redistribute it and/or modify it under
    5  * the terms of the GNU Library General Public License as published by
    6  * the Free Software Foundation; either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful, but
   10  * WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12  * General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU Library General Public
   15  * License along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   17  *
   18  * Red Hat Author(s): Nalin Dahyabhai, Behdad Esfahbod
   19  */
   20 
   21 #include <config.h>
   22 
   23 #include "debug.h"
   24 #include "vterowdata.h"
   25 
   26 #include <string.h>
   27 
   28 
   29 /*
   30  * VteCells: A row's cell array
   31  */
   32 
   33 typedef struct _VteCells VteCells;
   34 struct _VteCells {
   35     guint32 alloc_len;
   36     VteCell cells[1];
   37 };
   38 
   39 static inline VteCells *
   40 _vte_cells_for_cell_array (VteCell *cells)
   41 {
   42     if (G_UNLIKELY (!cells))
   43         return NULL;
   44 
   45     return (VteCells *) (((guchar *) cells) - G_STRUCT_OFFSET (VteCells, cells));
   46 }
   47 
   48 static VteCells *
   49 _vte_cells_realloc (VteCells *cells, guint32 len)
   50 {
   51     guint32 alloc_len = (1 << g_bit_storage (MAX (len, 80))) - 1;
   52 
   53     _vte_debug_print(VTE_DEBUG_RING, "Enlarging cell array of %d cells to %d cells\n", cells ? cells->alloc_len : 0, alloc_len);
   54     cells = g_realloc (cells, G_STRUCT_OFFSET (VteCells, cells) + alloc_len * sizeof (cells->cells[0]));
   55     cells->alloc_len = alloc_len;
   56 
   57     return cells;
   58 }
   59 
   60 static void
   61 _vte_cells_free (VteCells *cells)
   62 {
   63     _vte_debug_print(VTE_DEBUG_RING, "Freeing cell array of %d cells\n", cells->alloc_len);
   64     g_free (cells);
   65 }
   66 
   67 
   68 /*
   69  * VteRowData: A row's data
   70  */
   71 
   72 void
   73 _vte_row_data_init (VteRowData *row)
   74 {
   75     memset (row, 0, sizeof (*row));
   76 }
   77 
   78 void
   79 _vte_row_data_clear (VteRowData *row)
   80 {
   81     VteCell *cells = row->cells;
   82     _vte_row_data_init (row);
   83     row->cells = cells;
   84 }
   85 
   86 void
   87 _vte_row_data_fini (VteRowData *row)
   88 {
   89     if (row->cells)
   90         _vte_cells_free (_vte_cells_for_cell_array (row->cells));
   91     row->cells = NULL;
   92 }
   93 
   94 static inline gboolean
   95 _vte_row_data_ensure (VteRowData *row, gulong len)
   96 {
   97     VteCells *cells = _vte_cells_for_cell_array (row->cells);
   98     if (G_LIKELY (cells && len <= cells->alloc_len))
   99         return TRUE;
  100 
  101     if (G_UNLIKELY (len >= 0xFFFF))
  102         return FALSE;
  103 
  104     row->cells = _vte_cells_realloc (cells, len)->cells;
  105 
  106     return TRUE;
  107 }
  108 
  109 void
  110 _vte_row_data_insert (VteRowData *row, gulong col, const VteCell *cell)
  111 {
  112     gulong i;
  113 
  114     if (G_UNLIKELY (!_vte_row_data_ensure (row, row->len + 1)))
  115         return;
  116 
  117     for (i = row->len; i > col; i--)
  118         row->cells[i] = row->cells[i - 1];
  119 
  120     row->cells[col] = *cell;
  121     row->len++;
  122 }
  123 
  124 void _vte_row_data_append (VteRowData *row, const VteCell *cell)
  125 {
  126     if (G_UNLIKELY (!_vte_row_data_ensure (row, row->len + 1)))
  127         return;
  128 
  129     row->cells[row->len] = *cell;
  130     row->len++;
  131 }
  132 
  133 void _vte_row_data_remove (VteRowData *row, gulong col)
  134 {
  135     gulong i;
  136 
  137     for (i = col + 1; i < row->len; i++)
  138         row->cells[i - 1] = row->cells[i];
  139 
  140     if (G_LIKELY (row->len))
  141         row->len--;
  142 }
  143 
  144 void _vte_row_data_fill (VteRowData *row, const VteCell *cell, gulong len)
  145 {
  146     if (row->len < len) {
  147         gulong i = len - row->len;
  148 
  149         if (G_UNLIKELY (!_vte_row_data_ensure (row, len)))
  150             return;
  151 
  152         for (i = row->len; i < len; i++)
  153             row->cells[i] = *cell;
  154 
  155         row->len = len;
  156     }
  157 }
  158 
  159 void _vte_row_data_shrink (VteRowData *row, gulong max_len)
  160 {
  161     if (max_len < row->len)
  162         row->len = max_len;
  163 }
  164