1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 02:06:01 +02:00

Cosmetics.

This commit is contained in:
Sergey Prigogin 2012-12-23 17:53:53 -08:00
parent 5c26443c41
commit 83d83c5165
5 changed files with 183 additions and 192 deletions

View file

@ -56,7 +56,7 @@ public class DBTest extends BaseTestCase {
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
db.close(); db.close();
if(!db.getLocation().delete()) { if (!db.getLocation().delete()) {
db.getLocation().deleteOnExit(); db.getLocation().deleteOnExit();
} }
db= null; db= null;
@ -87,15 +87,15 @@ public class DBTest extends BaseTestCase {
try { try {
new Database(tmp, ChunkCache.getSharedInstance(), 0, false); new Database(tmp, ChunkCache.getSharedInstance(), 0, false);
fail("A readonly file should not be openable with write-access"); fail("A readonly file should not be openable with write-access");
} catch(CoreException ioe) { } catch (CoreException e) {
// we expect to get a failure here // we expect to get a failure here
} }
/* check opening a readonly file for read access does not fail */ /* check opening a readonly file for read access does not fail */
try { try {
new Database(tmp, ChunkCache.getSharedInstance(), 0, true); new Database(tmp, ChunkCache.getSharedInstance(), 0, true);
} catch(CoreException ce) { } catch (CoreException e) {
fail("A readonly file should be readable by a permanently readonly database "+ce); fail("A readonly file should be readable by a permanently readonly database "+e);
} }
} finally { } finally {
tmp.delete(); // this may be pointless on some platforms tmp.delete(); // this may be pointless on some platforms
@ -104,7 +104,7 @@ public class DBTest extends BaseTestCase {
public void testFreeBlockLinking() throws Exception { public void testFreeBlockLinking() throws Exception {
final int realsize = 42; final int realsize = 42;
final int deltas = (realsize+Database.BLOCK_HEADER_SIZE + Database.BLOCK_SIZE_DELTA - 1) / Database.BLOCK_SIZE_DELTA; final int deltas = (realsize + Database.BLOCK_HEADER_SIZE + Database.BLOCK_SIZE_DELTA - 1) / Database.BLOCK_SIZE_DELTA;
final int blocksize = deltas * Database.BLOCK_SIZE_DELTA; final int blocksize = deltas * Database.BLOCK_SIZE_DELTA;
final int freeDeltas= Database.MIN_BLOCK_DELTAS-deltas; final int freeDeltas= Database.MIN_BLOCK_DELTAS-deltas;
@ -150,7 +150,6 @@ public class DBTest extends BaseTestCase {
public long getRecord() { public long getRecord() {
return record; return record;
} }
} }
public void testStringsInBTree() throws Exception { public void testStringsInBTree() throws Exception {
@ -194,6 +193,7 @@ public class DBTest extends BaseTestCase {
return string1.compare(string2, true); return string1.compare(string2, true);
} }
}; };
BTree btree = new BTree(db, Database.DATA_AREA, comparator); BTree btree = new BTree(db, Database.DATA_AREA, comparator);
for (int i = 0; i < names.length; ++i) { for (int i = 0; i < names.length; ++i) {
String name = names[i]; String name = names[i];
@ -224,8 +224,8 @@ public class DBTest extends BaseTestCase {
assertCMP("", EQ, "", true); assertCMP("", EQ, "", true);
assertCMP("", EQ, "", false); assertCMP("", EQ, "", false);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH/2, r, true); doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH / 2, r, true);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH/2, r, false); doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH / 2, r, false);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, true); doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, true);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, false); doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, false);
@ -243,13 +243,13 @@ public class DBTest extends BaseTestCase {
public void testLongStringComparison() throws CoreException { public void testLongStringComparison() throws CoreException {
Random r= new Random(314159265); Random r= new Random(314159265);
doTrials(100, ShortString.MAX_BYTE_LENGTH+1, ShortString.MAX_BYTE_LENGTH*2, r, true); doTrials(100, ShortString.MAX_BYTE_LENGTH + 1, ShortString.MAX_BYTE_LENGTH * 2, r, true);
doTrials(100, ShortString.MAX_BYTE_LENGTH+1, ShortString.MAX_BYTE_LENGTH*2, r, false); doTrials(100, ShortString.MAX_BYTE_LENGTH + 1, ShortString.MAX_BYTE_LENGTH * 2, r, false);
} }
private void doTrials(int n, int min, int max, Random r, boolean caseSensitive) throws CoreException { private void doTrials(int n, int min, int max, Random r, boolean caseSensitive) throws CoreException {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
for(int i=0; i<n; i++) { for(int i= 0; i < n; i++) {
String a = randomString(min, max, r); String a = randomString(min, max, r);
String b = randomString(min, max, r); String b = randomString(min, max, r);
int expected = caseSensitive ? a.compareTo(b) : a.compareToIgnoreCase(b); int expected = caseSensitive ? a.compareTo(b) : a.compareToIgnoreCase(b);
@ -262,7 +262,7 @@ public class DBTest extends BaseTestCase {
private String randomString(int min, int max, Random r) { private String randomString(int min, int max, Random r) {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
int len = min + r.nextInt(max-min); int len = min + r.nextInt(max-min);
for(int i=0; i<len; i++) { for(int i= 0; i < len; i++) {
result.append(randomChar(r)); result.append(randomChar(r));
} }
return result.toString(); return result.toString();
@ -317,8 +317,8 @@ public class DBTest extends BaseTestCase {
} }
private void assertSignEquals(int a, int b) { private void assertSignEquals(int a, int b) {
a= a<0 ? -1 : (a>0 ? 1 : 0); a= a < 0 ? -1 : (a > 0 ? 1 : 0);
b= b<0 ? -1 : (b>0 ? 1 : 0); b= b < 0 ? -1 : (b > 0 ? 1 : 0);
assertEquals(a, b); assertEquals(a, b);
} }
} }

View file

@ -8,7 +8,6 @@
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests; package org.eclipse.cdt.internal.pdom.tests;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -32,8 +31,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
/** /**
* Tests for verifying whether the PDOM correctly stores information about * Tests for verifying whether the PDOM correctly stores information about C++ namespaces.
* C++ namespaces.
* *
* @author Vivian Kong * @author Vivian Kong
*/ */
@ -65,7 +63,7 @@ public class NamespaceTests extends PDOMTestBase {
} }
public void testAlias() throws Exception { public void testAlias() throws Exception {
/* Find all the namespace */ // Find all the namespace
IBinding[] namespaces = pdom.findBindings(Pattern.compile("namespace1"), false, INDEX_FILTER, NULL_MONITOR); IBinding[] namespaces = pdom.findBindings(Pattern.compile("namespace1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length); assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace); assertTrue(namespaces[0] instanceof ICPPNamespace);
@ -82,24 +80,21 @@ public class NamespaceTests extends PDOMTestBase {
assertTrue(namespaces[0] instanceof ICPPNamespaceAlias); assertTrue(namespaces[0] instanceof ICPPNamespaceAlias);
ICPPNamespaceAlias namespaceAlias = (ICPPNamespaceAlias) namespaces[0]; ICPPNamespaceAlias namespaceAlias = (ICPPNamespaceAlias) namespaces[0];
//TODO PDOM has no alias information // TODO PDOM has no alias information
// namespace2 and namespaceAlias should be referencing the same namespace // namespace2 and namespaceAlias should be referencing the same namespace
assertEquals(namespace2, namespaceAlias.getBinding()); assertEquals(namespace2, namespaceAlias.getBinding());
} }
public void testNested() throws Exception { public void testNested() throws Exception {
// Find deeply nested namespace
/* Find deeply nested namespace */
Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("namespace3")}; Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("namespace3")};
IBinding[] namespaces = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR); IBinding[] namespaces = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length); assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace); assertTrue(namespaces[0] instanceof ICPPNamespace);
} }
public void testMemberDefinition() throws Exception { public void testMemberDefinition() throws Exception {
// Find the definition of a member declared in a namespace
/* Find the definition of a member declared in a namespace */
Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("foo")}; Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("foo")};
IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR); IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, members.length); assertEquals(1, members.length);
@ -108,30 +103,27 @@ public class NamespaceTests extends PDOMTestBase {
IName[] decls = pdom.findNames(members[0], IIndex.FIND_DECLARATIONS); IName[] decls = pdom.findNames(members[0], IIndex.FIND_DECLARATIONS);
assertEquals(1, decls.length); assertEquals(1, decls.length);
IASTFileLocation loc = decls[0].getFileLocation(); IASTFileLocation loc = decls[0].getFileLocation();
assertEquals(offset("namespace.cpp", "void foo()") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("namespace.cpp", "void foo()") + 5, loc.getNodeOffset()); // character offset
IName[] defs = pdom.findNames(members[0], IIndex.FIND_DEFINITIONS); IName[] defs = pdom.findNames(members[0], IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length); assertEquals(1, defs.length);
loc = defs[0].getFileLocation(); loc = defs[0].getFileLocation();
assertEquals(offset("namespace.cpp", "::foo()") + 2, loc.getNodeOffset()); //character offset assertEquals(offset("namespace.cpp", "::foo()") + 2, loc.getNodeOffset()); // character offset
} }
public void testExtend() throws Exception { public void testExtend() throws Exception {
// Extending a namespace
/* Extending a namespace */
IBinding[] namespaces = pdom.findBindings(Pattern.compile("ns1"), false, INDEX_FILTER, NULL_MONITOR); IBinding[] namespaces = pdom.findBindings(Pattern.compile("ns1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length); assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace); assertTrue(namespaces[0] instanceof ICPPNamespace);
ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0]; ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0];
Pattern[] patterns = {Pattern.compile("ns1"), Pattern.compile("c")}; Pattern[] patterns = {Pattern.compile("ns1"), Pattern.compile("c")};
IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR); IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, members.length); //c was added by extending the namespace assertEquals(1, members.length); // c was added by extending the namespace
} }
public void testOverload() throws Exception { public void testOverload() throws Exception {
// Function overloading in namespace
//Function overloading in namespace
Pattern[] patterns = {Pattern.compile("ns3"), Pattern.compile("blah")}; Pattern[] patterns = {Pattern.compile("ns3"), Pattern.compile("blah")};
IBinding[] functions = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR); IBinding[] functions = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length); assertEquals(1, functions.length);
@ -141,23 +133,21 @@ public class NamespaceTests extends PDOMTestBase {
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS); IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length); assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation(); IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("overload.cpp","void blah(char)") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("overload.cpp", "void blah(char)") + 5, loc.getNodeOffset()); // character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS); IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(1, decls.length); assertEquals(1, decls.length);
loc = decls[0].getFileLocation(); loc = decls[0].getFileLocation();
assertEquals(offset("overload.cpp","void blah(char)") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("overload.cpp", "void blah(char)") + 5, loc.getNodeOffset()); // character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES); IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length); assertEquals(1, refs.length);
loc = refs[0].getFileLocation(); loc = refs[0].getFileLocation();
assertEquals(offset("overload.cpp","blah('a')"), loc.getNodeOffset()); //character offset assertEquals(offset("overload.cpp", "blah('a')"), loc.getNodeOffset()); // character offset
} }
public void testUnnamed() throws Exception { public void testUnnamed_162226() throws Exception {
// test case for Bugzilla 162226 // Unnamed Namespace
/* Unnamed Namespace */
IBinding[] functions = pdom.findBindings(Pattern.compile("function1"), true, INDEX_FILTER, NULL_MONITOR); IBinding[] functions = pdom.findBindings(Pattern.compile("function1"), true, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length); assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction); assertTrue(functions[0] instanceof ICPPFunction);
@ -166,23 +156,21 @@ public class NamespaceTests extends PDOMTestBase {
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS); IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length); assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation(); IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("unnamed.cpp","void function1()") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("unnamed.cpp", "void function1()") + 5, loc.getNodeOffset()); // character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS); IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(1, decls.length); assertEquals(1, decls.length);
loc = decls[0].getFileLocation(); loc = decls[0].getFileLocation();
assertEquals(offset("unnamed.cpp","void function1()") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("unnamed.cpp", "void function1()") + 5, loc.getNodeOffset()); // character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES); IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length); assertEquals(1, refs.length);
loc = refs[0].getFileLocation(); loc = refs[0].getFileLocation();
assertEquals(offset("unnamed.cpp","function1();"), loc.getNodeOffset()); //character offset assertEquals(offset("unnamed.cpp", "function1();"), loc.getNodeOffset()); // character offset
} }
public void testFriend() throws Exception { public void testFriend_162011() throws Exception {
/* Friend in namespace - function2 is not in Class1*/ // Friend in namespace - function2 is not in Class1
// Bugzilla 162011
IBinding[] functions = pdom.findBindings(Pattern.compile("function2"), false, INDEX_FILTER, NULL_MONITOR); IBinding[] functions = pdom.findBindings(Pattern.compile("function2"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length); assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction); assertTrue(functions[0] instanceof ICPPFunction);
@ -191,22 +179,21 @@ public class NamespaceTests extends PDOMTestBase {
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS); IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length); assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation(); IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("friend.cpp","void function2(Class1){};") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("friend.cpp", "void function2(Class1){};") + 5, loc.getNodeOffset()); // character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS); IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS);
assertEquals(1, decls.length); assertEquals(1, decls.length);
loc = decls[0].getFileLocation(); loc = decls[0].getFileLocation();
assertEquals(offset("friend.cpp","friend void function2(Class1);") + 12, loc.getNodeOffset()); //character offset assertEquals(offset("friend.cpp", "friend void function2(Class1);") + 12, loc.getNodeOffset()); // character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES); IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length); assertEquals(1, refs.length);
loc = refs[0].getFileLocation(); loc = refs[0].getFileLocation();
assertEquals(offset("friend.cpp","ns4::function2(element)") + 5, loc.getNodeOffset()); //character offset assertEquals(offset("friend.cpp", "ns4::function2(element)") + 5, loc.getNodeOffset()); // character offset
} }
public void testUsingDirective() throws Exception { public void testUsingDirective() throws Exception {
//TODO need to test for PDOM? or is it more for compiler? // TODO need to test for PDOM? or is it more for compiler?
Pattern[] patterns = {Pattern.compile("ns4"), Pattern.compile("element")}; Pattern[] patterns = {Pattern.compile("ns4"), Pattern.compile("element")};
IBinding[] variables = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR); IBinding[] variables = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, variables.length); assertEquals(1, variables.length);
@ -216,7 +203,7 @@ public class NamespaceTests extends PDOMTestBase {
IName[] defs = pdom.findNames(variable1, IIndex.FIND_DEFINITIONS); IName[] defs = pdom.findNames(variable1, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length); assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation(); IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("friend.cpp","Class1 element;") + 7, loc.getNodeOffset()); //character offset assertEquals(offset("friend.cpp", "Class1 element;") + 7, loc.getNodeOffset()); // character offset
IName[] decls = pdom.findNames(variable1, IIndex.FIND_DECLARATIONS); IName[] decls = pdom.findNames(variable1, IIndex.FIND_DECLARATIONS);
assertEquals(0, decls.length); assertEquals(0, decls.length);

View file

@ -65,7 +65,7 @@ import com.ibm.icu.text.MessageFormat;
* *
*/ */
public class Database { public class Database {
// public for tests only, you shouldn't need these // Public for tests only, you shouldn't need these.
public static final int INT_SIZE = 4; public static final int INT_SIZE = 4;
public static final int CHUNK_SIZE = 1024 * 4; public static final int CHUNK_SIZE = 1024 * 4;
public static final int OFFSET_IN_CHUNK_MASK= CHUNK_SIZE-1; public static final int OFFSET_IN_CHUNK_MASK= CHUNK_SIZE-1;
@ -82,7 +82,6 @@ public class Database {
public static final int ARGUMENT_SIZE = TYPE_SIZE; // size of a template argument in the database in bytes public static final int ARGUMENT_SIZE = TYPE_SIZE; // size of a template argument in the database in bytes
public static final long MAX_DB_SIZE= ((long) 1 << (Integer.SIZE + BLOCK_SIZE_DELTA_BITS)); public static final long MAX_DB_SIZE= ((long) 1 << (Integer.SIZE + BLOCK_SIZE_DELTA_BITS));
public static final int VERSION_OFFSET = 0; public static final int VERSION_OFFSET = 0;
public static final int DATA_AREA = (CHUNK_SIZE / BLOCK_SIZE_DELTA - MIN_BLOCK_DELTAS + 2) * INT_SIZE; public static final int DATA_AREA = (CHUNK_SIZE / BLOCK_SIZE_DELTA - MIN_BLOCK_DELTAS + 2) * INT_SIZE;
@ -92,8 +91,8 @@ public class Database {
private final File fLocation; private final File fLocation;
private final boolean fReadOnly; private final boolean fReadOnly;
private RandomAccessFile fFile; private RandomAccessFile fFile;
private boolean fExclusiveLock; // necessary for any write operation private boolean fExclusiveLock; // Necessary for any write operation.
private boolean fLocked; // necessary for any operation. private boolean fLocked; // Necessary for any operation.
private boolean fIsMarkedIncomplete; private boolean fIsMarkedIncomplete;
private int fVersion; private int fVersion;
@ -125,7 +124,7 @@ public class Database {
int nChunksOnDisk = (int) (fFile.length() / CHUNK_SIZE); int nChunksOnDisk = (int) (fFile.length() / CHUNK_SIZE);
fHeaderChunk= new Chunk(this, 0); fHeaderChunk= new Chunk(this, 0);
fHeaderChunk.fLocked= true; // never makes it into the cache, needed to satisfy assertions fHeaderChunk.fLocked= true; // Never makes it into the cache, needed to satisfy assertions.
if (nChunksOnDisk <= 0) { if (nChunksOnDisk <= 0) {
fVersion= version; fVersion= version;
fChunks= new Chunk[1]; fChunks= new Chunk[1];
@ -188,7 +187,7 @@ public class Database {
while (position < size) { while (position < size) {
nRead = from.transferTo(position, 4096 * 16, target); nRead = from.transferTo(position, 4096 * 16, target);
if (nRead == 0) { if (nRead == 0) {
break; // Should not happen break; // Should not happen.
} else { } else {
position+= nRead; position+= nRead;
} }
@ -214,14 +213,14 @@ public class Database {
removeChunksFromCache(); removeChunksFromCache();
fVersion= version; fVersion= version;
// clear the first chunk. // Clear the first chunk.
fHeaderChunk.clear(0, CHUNK_SIZE); fHeaderChunk.clear(0, CHUNK_SIZE);
// chunks have been removed from the cache, so we may just reset the array of chunks. // Chunks have been removed from the cache, so we may just reset the array of chunks.
fChunks = new Chunk[] {null}; fChunks = new Chunk[] {null};
fChunksUsed = fChunksAllocated = fChunks.length; fChunksUsed = fChunksAllocated = fChunks.length;
try { try {
fHeaderChunk.flush(); // zero out header chunk fHeaderChunk.flush(); // Zero out header chunk.
fFile.getChannel().truncate(CHUNK_SIZE); // truncate database fFile.getChannel().truncate(CHUNK_SIZE); // Truncate database.
} catch (IOException e) { } catch (IOException e) {
CCorePlugin.log(e); CCorePlugin.log(e);
} }
@ -296,14 +295,14 @@ public class Database {
*/ */
public long malloc(final int datasize) throws CoreException { public long malloc(final int datasize) throws CoreException {
assert fExclusiveLock; assert fExclusiveLock;
assert datasize >=0 && datasize <= MAX_MALLOC_SIZE; assert datasize >= 0 && datasize <= MAX_MALLOC_SIZE;
int needDeltas= (datasize + BLOCK_HEADER_SIZE + BLOCK_SIZE_DELTA - 1) / BLOCK_SIZE_DELTA; int needDeltas= (datasize + BLOCK_HEADER_SIZE + BLOCK_SIZE_DELTA - 1) / BLOCK_SIZE_DELTA;
if (needDeltas < MIN_BLOCK_DELTAS) { if (needDeltas < MIN_BLOCK_DELTAS) {
needDeltas= MIN_BLOCK_DELTAS; needDeltas= MIN_BLOCK_DELTAS;
} }
// Which block size // Which block size.
long freeblock = 0; long freeblock = 0;
int useDeltas; int useDeltas;
for (useDeltas= needDeltas; useDeltas <= MAX_BLOCK_DELTAS; useDeltas++) { for (useDeltas= needDeltas; useDeltas <= MAX_BLOCK_DELTAS; useDeltas++) {
@ -312,10 +311,10 @@ public class Database {
break; break;
} }
// get the block // Get the block.
Chunk chunk; Chunk chunk;
if (freeblock == 0) { if (freeblock == 0) {
// allocate a new chunk // Allocate a new chunk.
freeblock= createNewChunk(); freeblock= createNewChunk();
useDeltas = MAX_BLOCK_DELTAS; useDeltas = MAX_BLOCK_DELTAS;
chunk = getChunk(freeblock); chunk = getChunk(freeblock);
@ -326,16 +325,16 @@ public class Database {
final int unusedDeltas = useDeltas-needDeltas; final int unusedDeltas = useDeltas-needDeltas;
if (unusedDeltas >= MIN_BLOCK_DELTAS) { if (unusedDeltas >= MIN_BLOCK_DELTAS) {
// Add in the unused part of our block // Add in the unused part of our block.
addBlock(chunk, unusedDeltas*BLOCK_SIZE_DELTA, freeblock + needDeltas*BLOCK_SIZE_DELTA); addBlock(chunk, unusedDeltas*BLOCK_SIZE_DELTA, freeblock + needDeltas*BLOCK_SIZE_DELTA);
useDeltas= needDeltas; useDeltas= needDeltas;
} }
// Make our size negative to show in use // Make our size negative to show in use.
final int usedSize= useDeltas*BLOCK_SIZE_DELTA; final int usedSize= useDeltas*BLOCK_SIZE_DELTA;
chunk.putShort(freeblock, (short) -usedSize); chunk.putShort(freeblock, (short) -usedSize);
// Clear out the block, lots of people are expecting this // Clear out the block, lots of people are expecting this.
chunk.clear(freeblock + BLOCK_HEADER_SIZE, usedSize-BLOCK_HEADER_SIZE); chunk.clear(freeblock + BLOCK_HEADER_SIZE, usedSize-BLOCK_HEADER_SIZE);
malloced+= usedSize; malloced+= usedSize;
@ -419,7 +418,7 @@ public class Database {
long nextblock = chunk.getFreeRecPtr(block + BLOCK_NEXT_OFFSET); long nextblock = chunk.getFreeRecPtr(block + BLOCK_NEXT_OFFSET);
if (prevblock != 0) { if (prevblock != 0) {
putFreeRecPtr(prevblock + BLOCK_NEXT_OFFSET, nextblock); putFreeRecPtr(prevblock + BLOCK_NEXT_OFFSET, nextblock);
} else { // we were the head } else { // We were the head.
setFirstBlock(blocksize, nextblock); setFirstBlock(blocksize, nextblock);
} }
@ -432,7 +431,7 @@ public class Database {
// Mark our size // Mark our size
chunk.putShort(block, (short) blocksize); chunk.putShort(block, (short) blocksize);
// Add us to the head of the list // Add us to the head of the list.
long prevfirst = getFirstBlock(blocksize); long prevfirst = getFirstBlock(blocksize);
chunk.putFreeRecPtr(block + BLOCK_PREV_OFFSET, 0); chunk.putFreeRecPtr(block + BLOCK_PREV_OFFSET, 0);
chunk.putFreeRecPtr(block + BLOCK_NEXT_OFFSET, prevfirst); chunk.putFreeRecPtr(block + BLOCK_NEXT_OFFSET, prevfirst);
@ -448,13 +447,14 @@ public class Database {
*/ */
public void free(long offset) throws CoreException { public void free(long offset) throws CoreException {
assert fExclusiveLock; assert fExclusiveLock;
// TODO - look for opportunities to merge blocks // TODO Look for opportunities to merge blocks
long block = offset - BLOCK_HEADER_SIZE; long block = offset - BLOCK_HEADER_SIZE;
Chunk chunk = getChunk(block); Chunk chunk = getChunk(block);
int blocksize = - chunk.getShort(block); int blocksize = - chunk.getShort(block);
if (blocksize < 0) { if (blocksize < 0) {
// already freed // Already freed.
throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, 0, "Already Freed", new Exception())); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, 0,
"Already freed", new Exception())); //$NON-NLS-1$
} }
addBlock(chunk, blocksize, block); addBlock(chunk, blocksize, block);
freed += blocksize; freed += blocksize;
@ -614,7 +614,7 @@ public class Database {
flush(); flush();
removeChunksFromCache(); removeChunksFromCache();
// chunks have been removed from the cache, so we are fine // Chunks have been removed from the cache, so we are fine.
fHeaderChunk.clear(0, CHUNK_SIZE); fHeaderChunk.clear(0, CHUNK_SIZE);
fHeaderChunk.fDirty= false; fHeaderChunk.fDirty= false;
fChunks= new Chunk[] { null }; fChunks= new Chunk[] { null };
@ -672,15 +672,15 @@ public class Database {
Chunk chunk= fChunks[i]; Chunk chunk= fChunks[i];
if (chunk != null) { if (chunk != null) {
if (chunk.fCacheIndex < 0) { if (chunk.fCacheIndex < 0) {
// locked chunk that has been removed from cache. // Locked chunk that has been removed from cache.
if (chunk.fDirty) { if (chunk.fDirty) {
dirtyChunks.add(chunk); // keep in fChunks until it is flushed. dirtyChunks.add(chunk); // Keep in fChunks until it is flushed.
} else { } else {
chunk.fLocked= false; chunk.fLocked= false;
fChunks[i]= null; fChunks[i]= null;
} }
} else if (chunk.fLocked) { } else if (chunk.fLocked) {
// locked chunk, still in cache. // Locked chunk, still in cache.
if (chunk.fDirty) { if (chunk.fDirty) {
if (flush) { if (flush) {
dirtyChunks.add(chunk); dirtyChunks.add(chunk);
@ -689,12 +689,12 @@ public class Database {
chunk.fLocked= false; chunk.fLocked= false;
} }
} else { } else {
assert !chunk.fDirty; // dirty chunks must be locked. assert !chunk.fDirty; // Dirty chunks must be locked.
} }
} }
} }
} }
// also handles header chunk // Also handles header chunk.
flushAndUnlockChunks(dirtyChunks, flush); flushAndUnlockChunks(dirtyChunks, flush);
} finally { } finally {
fExclusiveLock= false; fExclusiveLock= false;
@ -713,7 +713,7 @@ public class Database {
return; return;
} }
// be careful as other readers may access chunks concurrently // Be careful as other readers may access chunks concurrently.
ArrayList<Chunk> dirtyChunks= new ArrayList<Chunk>(); ArrayList<Chunk> dirtyChunks= new ArrayList<Chunk>();
synchronized (fCache) { synchronized (fCache) {
for (int i= 1; i < fChunksUsed ; i++) { for (int i= 1; i < fChunksUsed ; i++) {
@ -724,7 +724,7 @@ public class Database {
} }
} }
// also handles header chunk // Also handles header chunk.
flushAndUnlockChunks(dirtyChunks, true); flushAndUnlockChunks(dirtyChunks, true);
} }
@ -742,7 +742,7 @@ public class Database {
} }
} }
// only after the chunks are flushed we may unlock and release them. // Only after the chunks are flushed we may unlock and release them.
synchronized (fCache) { synchronized (fCache) {
for (Chunk chunk : dirtyChunks) { for (Chunk chunk : dirtyChunks) {
chunk.fLocked= false; chunk.fLocked= false;

View file

@ -60,7 +60,8 @@ public class PDOMCPPClassTemplate extends PDOMCPPClassType
private volatile ICPPTemplateParameter[] params; // Cached template parameters. private volatile ICPPTemplateParameter[] params; // Cached template parameters.
public PDOMCPPClassTemplate(PDOMCPPLinkage linkage, PDOMNode parent, ICPPClassTemplate template) throws CoreException, DOMException { public PDOMCPPClassTemplate(PDOMCPPLinkage linkage, PDOMNode parent, ICPPClassTemplate template)
throws CoreException, DOMException {
super(linkage, parent, template); super(linkage, parent, template);
final Database db = getDB(); final Database db = getDB();
@ -127,7 +128,8 @@ public class PDOMCPPClassTemplate extends PDOMCPPClassType
} }
} }
private void updateTemplateParameters(PDOMLinkage linkage, ICPPTemplateParameter[] newParams) throws CoreException, DOMException { private void updateTemplateParameters(PDOMLinkage linkage, ICPPTemplateParameter[] newParams)
throws CoreException, DOMException {
final Database db = getDB(); final Database db = getDB();
long rec= db.getRecPtr(record + PARAMETERS); long rec= db.getRecPtr(record + PARAMETERS);
IPDOMCPPTemplateParameter[] allParams; IPDOMCPPTemplateParameter[] allParams;
@ -167,7 +169,7 @@ public class PDOMCPPClassTemplate extends PDOMCPPClassType
if (additionalPars > 0 || reorder) { if (additionalPars > 0 || reorder) {
params= null; params= null;
IPDOMCPPTemplateParameter[] newAllParams= new IPDOMCPPTemplateParameter[allParams.length+additionalPars]; IPDOMCPPTemplateParameter[] newAllParams= new IPDOMCPPTemplateParameter[allParams.length + additionalPars];
for (int j = 0; j < newParamLength; j++) { for (int j = 0; j < newParamLength; j++) {
int idx= result[j]; int idx= result[j];
if (idx >= 0) { if (idx >= 0) {

View file

@ -6,7 +6,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Markus Schorn - initial API and implementation * Markus Schorn - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp; package org.eclipse.cdt.internal.core.pdom.dom.cpp;
@ -22,7 +22,7 @@ import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
/** /**
* Collects methods to store an argument list in the database * Collects methods to store an argument list in the database.
*/ */
public class PDOMTemplateParameterArray { public class PDOMTemplateParameterArray {
/** /**
@ -30,12 +30,12 @@ public class PDOMTemplateParameterArray {
* @return the record by which the arguments can be referenced. * @return the record by which the arguments can be referenced.
*/ */
public static long putArray(final Database db, IPDOMCPPTemplateParameter[] params) throws CoreException { public static long putArray(final Database db, IPDOMCPPTemplateParameter[] params) throws CoreException {
final short len= (short) Math.min(params.length, (Database.MAX_MALLOC_SIZE-2)/8); final short len= (short) Math.min(params.length, (Database.MAX_MALLOC_SIZE - 2) / 8);
final long block= db.malloc(2+8*len); final long block= db.malloc(2 + 8 * len);
long p= block; long p= block;
db.putShort(p, len); p+=2; db.putShort(p, len); p += 2;
for (int i=0; i<len; i++, p+=4) { for (int i= 0; i < len; i++, p += 4) {
final IPDOMCPPTemplateParameter elem= params[i]; final IPDOMCPPTemplateParameter elem= params[i];
db.putRecPtr(p, elem == null ? 0 : elem.getRecord()); db.putRecPtr(p, elem == null ? 0 : elem.getRecord());
} }
@ -50,15 +50,15 @@ public class PDOMTemplateParameterArray {
final Database db= linkage.getDB(); final Database db= linkage.getDB();
final short len= db.getShort(rec); final short len= db.getShort(rec);
Assert.isTrue(len >= 0 && len <= (Database.MAX_MALLOC_SIZE-2)/8); Assert.isTrue(len >= 0 && len <= (Database.MAX_MALLOC_SIZE - 2) / 8);
if (len == 0) { if (len == 0) {
return IPDOMCPPTemplateParameter.EMPTY_ARRAY; return IPDOMCPPTemplateParameter.EMPTY_ARRAY;
} }
rec+=2; rec += 2;
IPDOMCPPTemplateParameter[] result= new IPDOMCPPTemplateParameter[len]; IPDOMCPPTemplateParameter[] result= new IPDOMCPPTemplateParameter[len];
for (int i=0; i<len; i++) { for (int i= 0; i < len; i++) {
final long nodeRec= db.getRecPtr(rec); rec+=4; final long nodeRec= db.getRecPtr(rec); rec += 4;
result[i]= nodeRec == 0 ? null : (IPDOMCPPTemplateParameter) linkage.getNode(nodeRec); result[i]= nodeRec == 0 ? null : (IPDOMCPPTemplateParameter) linkage.getNode(nodeRec);
} }
return result; return result;
@ -67,7 +67,8 @@ public class PDOMTemplateParameterArray {
/** /**
* Creates template parameters in the pdom * Creates template parameters in the pdom
*/ */
public static IPDOMCPPTemplateParameter[] createPDOMTemplateParameters(PDOMLinkage linkage, PDOMNode parent, ICPPTemplateParameter[] origParams) throws CoreException, DOMException { public static IPDOMCPPTemplateParameter[] createPDOMTemplateParameters(PDOMLinkage linkage,
PDOMNode parent, ICPPTemplateParameter[] origParams) throws CoreException, DOMException {
IPDOMCPPTemplateParameter[] params= new IPDOMCPPTemplateParameter[origParams.length]; IPDOMCPPTemplateParameter[] params= new IPDOMCPPTemplateParameter[origParams.length];
for (int i = 0; i < origParams.length; i++) { for (int i = 0; i < origParams.length; i++) {
params[i]= createPDOMTemplateParameter(linkage, parent, origParams[i]); params[i]= createPDOMTemplateParameter(linkage, parent, origParams[i]);
@ -78,7 +79,8 @@ public class PDOMTemplateParameterArray {
/** /**
* Creates a template parameter in the pdom * Creates a template parameter in the pdom
*/ */
public static IPDOMCPPTemplateParameter createPDOMTemplateParameter(PDOMLinkage linkage, PDOMNode parent, ICPPTemplateParameter origParam) throws CoreException, DOMException { public static IPDOMCPPTemplateParameter createPDOMTemplateParameter(PDOMLinkage linkage,
PDOMNode parent, ICPPTemplateParameter origParam) throws CoreException, DOMException {
IPDOMCPPTemplateParameter param= null; IPDOMCPPTemplateParameter param= null;
if (origParam instanceof ICPPTemplateNonTypeParameter) { if (origParam instanceof ICPPTemplateNonTypeParameter) {
param= new PDOMCPPTemplateNonTypeParameter(linkage, parent, (ICPPTemplateNonTypeParameter) origParam); param= new PDOMCPPTemplateNonTypeParameter(linkage, parent, (ICPPTemplateNonTypeParameter) origParam);