File: time-buffer.c

package info (click to toggle)
soundtracker 0.3.8-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,884 kB
  • ctags: 2,176
  • sloc: ansic: 19,640; sh: 327; makefile: 266; asm: 156; sed: 93
file content (120 lines) | stat: -rw-r--r-- 2,425 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

/*
 * The Real SoundTracker - time buffer
 *
 * Copyright (C) 1999 Michael Krause
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "time-buffer.h"

#include <glib.h>

/* This implementation of the time buffer interface might be rather
   suboptimal... */

struct time_buffer {
    GMutex *mutex;
    GList *list;
};

typedef struct time_buffer_item {
    double time;
    /* then user data follows */
} time_buffer_item;

time_buffer *
time_buffer_new (double maxtimedelta)
{
    time_buffer *t = g_new(time_buffer, 1);

    if(t) {
	t->mutex = g_mutex_new();
	t->list = NULL;
    }

    return t;
}

void
time_buffer_destroy (time_buffer *t)
{
    if(t) {
	g_list_free(t->list);
	g_mutex_free(t->mutex);
	g_free(t);
    }
}

void
time_buffer_clear (time_buffer *t)
{
    while(t->list) {
	g_free(t->list->data);
	t->list = g_list_remove_link(t->list, t->list);
    }
}

gboolean
time_buffer_add (time_buffer *t,
		 void *item,
		 double time)
{
    time_buffer_item *a = item;

    g_mutex_lock(t->mutex);
    a->time = time;
    t->list = g_list_append(t->list, a);
    g_mutex_unlock(t->mutex);

    return TRUE;
}

void *
time_buffer_get (time_buffer *t,
		 double time)
{
    int i, j, l;
    void *result = NULL;
    GList *list;

    g_mutex_lock(t->mutex);
    l = g_list_length(t->list);

    if(l == 0) {
	g_mutex_unlock(t->mutex);
	return NULL;
    }

    for(i = 0, list = t->list; i < l - 1; i++, list = list->next) {
     	time_buffer_item *a = list->data;
	if(time < a->time)
	    break;
    }

    for(j = 0; j < i - 1; j++) {
	GList *node = t->list;
	g_free(node->data);
	t->list = g_list_remove_link(t->list, node);
	g_list_free(node);
    }

    result = t->list->data;

    g_mutex_unlock(t->mutex);

    return result;
}