Skip to content

Commit

Permalink
gh-30 fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Jun 15, 2024
1 parent 2050f2b commit d6b6a15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public boolean isClosed() {
public void close() {
for (Http2Stream s : streams.values())
s.close(true);
streams.clear();
}

/**
Expand Down Expand Up @@ -146,10 +147,12 @@ private void readFrame(Buffers buf) throws InterruptedException {
return;
}

streams.set(id, s = new Http2Stream(co, id, this, initialWindow));
s = new Http2Stream(co, id, this, initialWindow);

if ((flags & 0x1) == 1) // END_STREAM
s.close(false);
else
streams.set(id, s);
if ((flags & 0x8) == 1) {
pad = buf.read(false);
if (pad >= size) {
Expand All @@ -162,7 +165,7 @@ private void readFrame(Buffers buf) throws InterruptedException {
buf.skip(5);
}

r = new FrameHeader(size, flags, id, pad).process(buf);
r = new FrameHeader(size, flags, id, pad, s).process(buf);
return;
case 2: // priority
r = new FrameReader(size, flags, id).process(buf);
Expand Down Expand Up @@ -238,11 +241,7 @@ private void readFrame(Buffers buf) throws InterruptedException {
return;
}

r = new FrameHeader(size, flags, id, pad).process(buf);

wantContinuation = (flags & 0x4) == 0;
if (!wantContinuation)
s.start();
r = new FrameHeader(size, flags, id, pad, s).process(buf);
return;
default:
goaway(PROTOCOL_ERROR);
Expand Down Expand Up @@ -502,6 +501,7 @@ protected FrameGoAway(int size, int flags, int id) {
b = new byte[4];
}

@Override
public FrameReader process(Buffers buf) throws InterruptedException {
if (lastId >= 0)
return super.process(buf);
Expand Down Expand Up @@ -552,9 +552,9 @@ private class FrameHeader extends FrameReader {
private final Buffers remain;
private int pad;

protected FrameHeader(int size, int flags, int id, int pad) {
protected FrameHeader(int size, int flags, int id, int pad, Http2Stream s) {
super(size, flags, id);
this.s = streams.get(id);
this.s = s;
this.remain = new Buffers();
this.pad = pad;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ public T get(int key) {
public T set(int key, T value) {
int i = Arrays.binarySearch(keys, 0, len, key);
if (i >= 0) {
T old = values[i];
values[i] = value;
return null;
return old;
}
ensure(++len);
i = -i - 1;
T old = values[i];
if (i < len - 1) {
System.arraycopy(keys, i, keys, i + 1, len - i - 1);
System.arraycopy(values, i, values, i + 1, len - i - 1);
}
keys[i] = key;
values[i] = value;
return old;
return null;
}

/**
Expand All @@ -91,12 +95,12 @@ public boolean setOnce(int key, T value) {
int i = Arrays.binarySearch(keys, 0, len, key);
if (i >= 0)
return false;
if (i < len) {
System.arraycopy(keys, i, keys, i + 1, len - i);
System.arraycopy(values, i, values, i + 1, len - i);
}
ensure(++len);
i = -i - 1;
if (i < len - 1) {
System.arraycopy(keys, i, keys, i + 1, len - i - 1);
System.arraycopy(values, i, values, i + 1, len - i - 1);
}
keys[i] = key;
values[i] = value;
return true;
Expand All @@ -107,7 +111,24 @@ public boolean setOnce(int key, T value) {
* @return the removed value
*/
public T remove(int key) {
return set(key, null);
int i = Arrays.binarySearch(keys, 0, len, key);
if (i < 0)
return null;
T old = values[i];
len--;
System.arraycopy(keys, i + 1, keys, i, len - i);
System.arraycopy(values, i + 1, values, i, len - i);
values[len] = null;
return old;
}

/**
* empty the map
*/
public void clear() {
for (int i = 0; i < len; i++)
values[i] = null;
len = 0;
}

/**
Expand Down

0 comments on commit d6b6a15

Please sign in to comment.