Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef SimpleBuffer_h__
8
#define SimpleBuffer_h__
9
10
/*
11
This class is similar to a nsPipe except it does not have any locking, stores
12
an unbounded amount of data, can only be used on one thread, and has much
13
simpler result code semantics to deal with.
14
*/
15
16
#include "prtypes.h"
17
#include "mozilla/LinkedList.h"
18
#include "nsIThreadInternal.h"
19
20
namespace mozilla {
21
namespace net {
22
23
class SimpleBufferPage : public LinkedListElement<SimpleBufferPage> {
24
public:
25
SimpleBufferPage() : mReadOffset(0), mWriteOffset(0) {}
26
static const size_t kSimpleBufferPageSize = 32000;
27
28
private:
29
friend class SimpleBuffer;
30
char mBuffer[kSimpleBufferPageSize];
31
size_t mReadOffset;
32
size_t mWriteOffset;
33
};
34
35
class SimpleBuffer {
36
public:
37
SimpleBuffer();
38
~SimpleBuffer() = default;
39
40
nsresult Write(char* stc, size_t len); // return OK or OUT_OF_MEMORY
41
size_t Read(char* dest, size_t maxLen); // return bytes read
42
size_t Available();
43
void Clear();
44
45
private:
46
NS_DECL_OWNINGTHREAD
47
48
nsresult mStatus;
49
AutoCleanLinkedList<SimpleBufferPage> mBufferList;
50
size_t mAvailable;
51
};
52
53
} // namespace net
54
} // namespace mozilla
55
56
#endif