summaryrefslogtreecommitdiff
path: root/src/otlayout/ot-array.c
blob: aaa286e26dd4bb14e97eb350aac797c008d4792e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* OTArray
 *
 * Copyright (C) 2003 Red Hat.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "ot-array.h"
#include FT_INTERNAL_MEMORY_H
#include FT_CONFIG_STANDARD_LIBRARY_H

#define OT_REALLOC_ARRAY( _pointer_, _old_, _new_, eltsize )	\
          FT_SET_ERROR( FT_MEM_REALLOC( _pointer_,		\
                                        (_old_) * eltsize,	\
                                        (_new_) * eltsize ) )

  FT_LOCAL_DEF( OTArray * ) 
  OT_Array_New ( FT_UInt element_size, FT_Memory memory )
  {
    FT_Error error;
    OTArray * array;
    
    if ( FT_NEW ( array ) )
      return NULL;

    array->data 	= NULL;
    array->length 	= 0;
    array->allocated    = 0;
    array->element_size = element_size;
    array->memory 	= memory;
    return OT_Array_Set_Size ( array, 4 );
  }

  FT_LOCAL_DEF( void )      
  OT_Array_Free ( OTArray * array )
  {
    FT_Memory memory = array->memory;
    FT_FREE( array->data );
    FT_FREE( array );
  }
  
  FT_LOCAL_DEF( OTArray * ) 
  OT_Array_Set_Size ( OTArray * array, FT_UInt length )
  {
    FT_Error error;
    FT_Memory memory = array->memory;

    if ( length > array->allocated )
      {
	if ( OT_REALLOC_ARRAY(array->data,
			      array->allocated,
			      length,
			      array->element_size) )
	  return NULL;
	array->allocated = length;
      }
    array->length = length;
    return array;
  }

  FT_LOCAL_DEF( void )
  OT_Array_Sort ( OTArray * array, OT_Array_Comapre_Func func )
  {
    ft_qsort ( array->data, array->length, array->element_size, func );
  }

  FT_LOCAL_DEF( OTArray * )
  OT_Array_Append_Val ( OTArray * array, FT_Pointer newval )
  {
    FT_UInt index;
    FT_Pointer dest;
    
    if ( !OT_Array_Set_Size( array, array->length + 1 ) )
      return NULL;

    index = array->length - 1;
    dest  = array->data + (  index * array->element_size );
    FT_MEM_COPY ( dest, newval, array->element_size );
    return array;
  }