File: jpeg_memory.cpp

package info (click to toggle)
tango 7.2.6%2Bdfsg-14
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 20,720 kB
  • sloc: cpp: 122,899; sh: 11,304; ansic: 1,079; makefile: 843; java: 215; python: 55
file content (162 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
///=============================================================================	
//
// file :		jpeg_memory.cpp
//
// description :        Simple jpeg coding/decoding library
//                      Small memory manager (16 bytes aligned for MMX/SSE code)
//
// project :		TANGO
//
// author(s) :		JL Pons
//
// Copyright (C) :      2004,2005,2006,2007,2008,2009,2010,2011
//                      European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Tango 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 Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
//
// $Revision: 16143 $
//
// $Log$
// Revision 1.4  2009/04/20 14:55:58  jlpons
// Added GPL header, changed memory allocation to C++ fashion.
//
//=============================================================================

#include "jpeg_memory.h"
#include <stdio.h>
#include <string.h>

#define MAX_BUFFER 50

typedef struct {

  void   *buff;
  void   *buff16;
  size_t  size;

} MEMORY_HEADER;

static int nb_allocated = 0;
static MEMORY_HEADER MemTable[MAX_BUFFER];

// Allocate a buffer (always 16 bytes aligned) 
void *malloc_16(size_t size) {

  void *mptr;
  void *mptr16;

  if( nb_allocated == MAX_BUFFER ) {
    printf("Warning: jpeg_memory number of buffer exeeded\n");
    return NULL;
  }

  mptr = malloc(size+16);

  if( !mptr ) {
    printf("Warning: jpeg_memory not enough free memory\n");
    return NULL;
  }

  MemTable[nb_allocated].buff=mptr;
  MemTable[nb_allocated].size=size;

  // Aligns memory block on 16byte
  mptr16 = ((void *)(((size_t)mptr + 15) & ~15));
  MemTable[nb_allocated].buff16=mptr16;
  nb_allocated++;

  return mptr16;
}

// Allocate a buffer (always 16 bytes aligned) 
void *calloc_16(size_t count,size_t size) {

  void *mptr;
  void *mptr16;

  if( nb_allocated == MAX_BUFFER )
    return NULL;

  mptr = malloc(size*count+16);

  if( !mptr ) return NULL;

  MemTable[nb_allocated].buff=mptr;
  MemTable[nb_allocated].size=size;

  // Aligns memory block on 16byte
  mptr16 = ((void *)(((size_t)mptr + 15) & ~15));
  MemTable[nb_allocated].buff16=mptr16;
  nb_allocated++;

  return mptr16;
}

// Free a buffer
void free_16( void *ptr ) {
  
  int i,found;

  if( ptr==NULL ) return;

  i=0;found=0;
  while(!found && i<nb_allocated ) {
    found = (MemTable[i].buff16==ptr);
    if(!found) i++;
  }

  if( found ) {
    free( MemTable[i].buff );
    nb_allocated--;
//    memcpy( &(MemTable[i]) , &(MemTable[i+1]) , sizeof(MEMORY_HEADER)*(nb_allocated-i) );
	for(int j=i;j<nb_allocated;j++) MemTable[j] = MemTable[j+1]; 
  } else {
    printf("jpeg_memory warning: Trying to free unallocated memory");
  }

}

//Return the size of a buffer previoulsy allocated by malloc_16
size_t _msize_16( void *ptr ) {

  int i,found;

  if( ptr==NULL ) return 0;

  i=0;found=0;
  while(!found && i<nb_allocated ) {
    found = (MemTable[i].buff16==ptr);
    if(!found) i++;
  }

  if( found ) {
    return MemTable[i].size;
  } else {
    return 0;
  }

}

// Free all allocated buffer
void free_all_16() {

  for(int i=0;i<nb_allocated;i++)
    free( MemTable[i].buff );
  nb_allocated = 0;

}