/[MITgcm]/mitgcm.org/devel/buildweb/pkg/swish-e/src/ramdisk.c
ViewVC logotype

Annotation of /mitgcm.org/devel/buildweb/pkg/swish-e/src/ramdisk.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (hide annotations) (download) (vendor branch)
Fri Sep 20 19:47:29 2002 UTC (22 years, 10 months ago) by adcroft
Branch: Import, MAIN
CVS Tags: baseline, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
Importing web-site building process.

1 adcroft 1.1 /*
2     **
3     ** This program and library is free software; you can redistribute it and/or
4     ** modify it under the terms of the GNU (Library) General Public License
5     ** as published by the Free Software Foundation; either version 2
6     ** of the License, or any later version.
7     **
8     ** This program is distributed in the hope that it will be useful,
9     ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10     ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11     ** GNU (Library) General Public License for more details.
12     **
13     ** You should have received a copy of the GNU (Library) General Public License
14     ** along with this program; if not, write to the Free Software
15     ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16     **
17     **
18     ** 2001-06-21 jmruiz ramdisk module or "poor man ramdisk" - Initial coding
19     ** to test with LOCATIONS but it can be used with other
20     ** things.
21     ** Writes: Only sequential write is allowed
22     ** Reads: Direct and sequential reads are allowed
23     ** Routines and its equivalents:
24     **
25     ** ramdisk_create fopen
26     ** ramdisk_tell ftell
27     ** ramdisk_write fwrite
28     ** ramdisk_read fwrite
29     ** ramdisk_seek fseek
30     ** ramdisk_close fclose
31     **
32     **
33     */
34    
35    
36     #include "swish.h"
37     #include "mem.h"
38     #include "ramdisk.h"
39    
40     /* 06/2001 Jose Ruiz
41     ** Routines to store/extract location data in memory incontigous positions to sa
42     ve
43     ** memory.
44     ** The data has been compressed previously in memory.
45     ** Returns the pointer to the memory.
46     */
47    
48     struct ramdisk
49     {
50     unsigned long cur_pos;
51     unsigned long end_pos;
52     unsigned int n_buffers;
53     unsigned int buf_size;
54     unsigned char **buffer;
55     MEM_ZONE *zone;
56     };
57    
58    
59     /* Create a new ramdisk - The size of the internal buffers is buf_size */
60     struct ramdisk *ramdisk_create(char *name, int buf_size)
61     {
62     struct ramdisk *rd;
63    
64     rd = (struct ramdisk *) emalloc(sizeof(struct ramdisk));
65     rd->zone = Mem_ZoneCreate(name, buf_size, 0);
66     rd->cur_pos = 0;
67     rd->end_pos =0;
68     rd->n_buffers = 1;
69     rd->buf_size = buf_size;
70     rd->buffer = (unsigned char **)emalloc(sizeof(unsigned char *));
71     rd->buffer[0] = (unsigned char *)Mem_ZoneAlloc(rd->zone, buf_size);
72     return rd;
73     }
74    
75     /* Closes / frees the memory used by the ramdisk */
76     int ramdisk_close(FILE *fp)
77     {
78     struct ramdisk *rd = (struct ramdisk *)fp;
79    
80     Mem_ZoneFree(&rd->zone);
81     efree(rd->buffer);
82     efree(rd);
83     return 0;
84     }
85    
86     void add_buffer_ramdisk(struct ramdisk *rd)
87     {
88     rd->buffer = (unsigned char **)erealloc(rd->buffer,(rd->n_buffers + 1) * sizeof(unsigned char *));
89     rd->buffer[rd->n_buffers++] = (unsigned char *)Mem_ZoneAlloc(rd->zone, rd->buf_size);
90     }
91    
92     /* Equivalent to ftell to get the position while writing to the ramdisk */
93     long ramdisk_tell(FILE *fp)
94     {
95     struct ramdisk *rd = (struct ramdisk *)fp;
96    
97     return rd->cur_pos;
98     }
99    
100    
101     /* Writes to the ramdisk - The parameters are identical to those in fwrite */
102     size_t ramdisk_write(const void *buffer,size_t sz1, size_t sz2, FILE *fp)
103     {
104     unsigned int lenbuf=(unsigned int)(sz1 *sz2);
105     struct ramdisk *rd = (struct ramdisk *)fp;
106     unsigned char *buf = (unsigned char *)buffer;
107     unsigned int num_buffer,start_pos,tmplenbuf = lenbuf;
108     unsigned int avail;
109    
110     num_buffer = rd->cur_pos / rd->buf_size;
111     start_pos = rd->cur_pos % rd->buf_size;
112    
113     avail = rd->buf_size - start_pos;
114     while(avail<=(unsigned int)lenbuf)
115     {
116     if(avail)
117     memcpy(rd->buffer[num_buffer]+start_pos,buf,avail);
118     lenbuf -= avail;
119     rd->cur_pos += avail;
120     buf += avail;
121     add_buffer_ramdisk(rd);
122     avail = rd->buf_size;
123     start_pos = 0;
124     num_buffer++;
125     }
126     if(lenbuf)
127     {
128     memcpy(rd->buffer[num_buffer]+start_pos,buf,lenbuf);
129     rd->cur_pos += lenbuf;
130     }
131     if(rd->cur_pos > rd->end_pos)
132     rd->end_pos = rd->cur_pos;
133     return (int) tmplenbuf;
134     }
135    
136     /* Equivalent to fseek */
137     int ramdisk_seek(FILE *fp,long pos, int set)
138     {
139     struct ramdisk *rd = (struct ramdisk *)fp;
140    
141     switch(set)
142     {
143     case SEEK_CUR:
144     pos += rd->cur_pos;
145     break;
146     case SEEK_END:
147     pos += rd->end_pos;
148     break;
149     }
150     if( pos > rd->end_pos )
151     {
152     while(rd->end_pos < pos)
153     {
154     ramdisk_putc(0, (FILE *)rd);
155     }
156     }
157     else
158     {
159     rd->cur_pos = pos;
160     }
161     return 0;
162     }
163    
164    
165     /* Reads from the ramdisk - The parameters are identical to those in fread */
166     size_t ramdisk_read(void *buf, size_t sz1, size_t sz2, FILE *fp)
167     {
168     struct ramdisk *rd = (struct ramdisk *)fp;
169     unsigned int len = (unsigned int) (sz1 *sz2);
170     unsigned char *buffer = (unsigned char *)buf;
171     unsigned int avail, num_buffer, start_pos, buffer_offset;
172    
173     if(rd->cur_pos >= rd->end_pos)
174     return 0;
175     if((rd->cur_pos + len) > rd->end_pos)
176     {
177     len = rd->end_pos - rd->cur_pos;
178     }
179     num_buffer = rd->cur_pos / rd->buf_size;
180     start_pos = rd->cur_pos % rd->buf_size;
181    
182     buffer_offset = 0;
183    
184     avail = rd->buf_size - start_pos;
185     while(avail < (unsigned int)len)
186     {
187     memcpy(buffer+buffer_offset,rd->buffer[num_buffer]+start_pos,avail);
188     buffer_offset += avail;
189     rd->cur_pos += avail;
190     len -= avail;
191     num_buffer++;
192     start_pos=0;
193     avail = rd->buf_size;
194     if(num_buffer == rd->n_buffers)
195     return buffer_offset;
196     }
197     memcpy(buffer+buffer_offset,rd->buffer[num_buffer]+start_pos,len);
198     rd->cur_pos += len;
199     buffer_offset += len;
200     return buffer_offset;
201     }
202    
203     int ramdisk_getc(FILE *fp)
204     {
205     unsigned char c;
206    
207     ramdisk_read((void *)&c, 1, 1, fp);
208     return (int) ((unsigned char)c);
209     }
210    
211     int ramdisk_putc(int c, FILE *fp)
212     {
213     unsigned char tmp = (unsigned char)c;
214    
215     ramdisk_write((const void *)&tmp,1, 1, fp);
216     return 1;
217     }

  ViewVC Help
Powered by ViewVC 1.1.22