~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/util/timeStampArray.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  class for managing byte positions and associated time positions
 
3
  Copyright (C) 1999  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include "timeStampArray.h"
 
15
 
 
16
 
 
17
 
 
18
TimeStampArray::TimeStampArray(char* aName,int entries) {
 
19
 
 
20
  writePos=0;
 
21
  readPos=0;
 
22
  fillgrade=0;
 
23
  lastWritePos=0;
 
24
  this->entries=entries;
 
25
  if (entries < 1) {
 
26
    cout << "TimeStampArray entries must be >= 1";
 
27
    exit(0);
 
28
  }
 
29
 
 
30
 
 
31
  abs_thread_mutex_init(&writeInMut);
 
32
  abs_thread_mutex_init(&changeMut);
 
33
 
 
34
  
 
35
 
 
36
  name=strdup(aName);
 
37
  int i;
 
38
  tStampArray=new TimeStamp*[entries];
 
39
 
 
40
  for(i=0;i<entries;i++) {
 
41
    tStampArray[i]=new TimeStamp();
 
42
  }
 
43
 
 
44
}
 
45
 
 
46
 
 
47
TimeStampArray::~TimeStampArray() {
 
48
 
 
49
  int i;
 
50
  for(i=0;i<entries;i++) {
 
51
    delete tStampArray[i];
 
52
  } 
 
53
  delete tStampArray;
 
54
  if (name != NULL) {
 
55
    delete name;  // allocated with strdup
 
56
  }
 
57
  abs_thread_mutex_destroy(&writeInMut);
 
58
  abs_thread_mutex_destroy(&changeMut);
 
59
}
 
60
 
 
61
TimeStamp*  TimeStampArray::getReadTimeStamp() {
 
62
  return tStampArray[readPos];
 
63
}
 
64
 
 
65
 
 
66
int TimeStampArray::getFillgrade() {
 
67
  return fillgrade;
 
68
}
 
69
 
 
70
int TimeStampArray::insertTimeStamp(TimeStamp* src,long key,int len) {
 
71
  if (src == NULL) {
 
72
     return true;
 
73
  }
 
74
  lockStampArray();
 
75
  int back=true;
 
76
  src->copyTo(tStampArray[writePos]);
 
77
  tStampArray[writePos]->setKey(key,len);
 
78
  /*
 
79
  if (fillgrade > 0) {
 
80
    if (tStampArray[lastWritePos]->getKey() == key) {
 
81
      unlockStampArray();
 
82
      return;
 
83
    }
 
84
  }
 
85
  */
 
86
 
 
87
  lastWritePos=writePos;
 
88
  writePos++;
 
89
  fillgrade++;
 
90
  if (writePos == entries) {
 
91
    writePos=0;
 
92
  }
 
93
  if (fillgrade == entries) {
 
94
    cout << name<<" TimeStampArray::array overfull forward"<<endl;
 
95
    internalForward();
 
96
    back=false;
 
97
  }
 
98
  unlockStampArray();
 
99
  return back;
 
100
}
 
101
 
 
102
 
 
103
int TimeStampArray::bytesUntilNext(long key) {
 
104
  lockStampArray();
 
105
  TimeStamp* current=tStampArray[readPos];
 
106
  int back=current->getKey()-key;
 
107
  unlockStampArray();
 
108
  return back;
 
109
}
 
110
 
 
111
TimeStamp* TimeStampArray::getTimeStamp(long key) {
 
112
  lockStampArray();
 
113
  TimeStamp* back=tStampArray[readPos];
 
114
  if (key > back->getKey()+back->getKeyLen()) {
 
115
    if (fillgrade > 1) {
 
116
      internalForward();
 
117
      unlockStampArray();
 
118
      return getTimeStamp(key);
 
119
    }
 
120
  } 
 
121
 
 
122
  /*
 
123
  if (back->getKey() > key) {
 
124
    cout << "key "<<key<<" too big"<<back->getKey()-key<<endl;
 
125
    back->print("key access");
 
126
  } 
 
127
  */
 
128
  unlockStampArray();
 
129
  /* maybe we should return NULL here to indicate
 
130
     that there is no valid timestamp */
 
131
  /* This would need a check for every getTimeStamp call
 
132
     I think returning the last available stamp is ok
 
133
  */
 
134
  return back;
 
135
}
 
136
 
 
137
 
 
138
void TimeStampArray::forward() {
 
139
  lockStampArray();
 
140
  internalForward();
 
141
  unlockStampArray();
 
142
}
 
143
 
 
144
 
 
145
 
 
146
void TimeStampArray::clear() {
 
147
  lockStampArray();
 
148
  writePos=0;
 
149
  readPos=0;
 
150
  fillgrade=0;
 
151
  unlockStampArray();
 
152
}
 
153
 
 
154
 
 
155
void TimeStampArray::lockStampArray() {
 
156
 
 
157
  abs_thread_mutex_lock(&changeMut);
 
158
  abs_thread_mutex_lock(&writeInMut);
 
159
  abs_thread_mutex_unlock(&changeMut);
 
160
 
 
161
}
 
162
 
 
163
 
 
164
void TimeStampArray::unlockStampArray() {
 
165
  abs_thread_mutex_unlock(&writeInMut); 
 
166
}
 
167
 
 
168
 
 
169
void TimeStampArray::internalForward() {
 
170
  readPos++;
 
171
  fillgrade--;
 
172
  if (readPos == entries) {
 
173
    readPos=0;
 
174
  }
 
175
}