JVM class format: why is `constant_pool_count` bigger than the" should "be?

The JVMclass format uses constant_pool_countto indicate the size of the pool of class constants:

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

Where is constant_pool_countdefined as

The value of an element constant_pool_countis equal to the number of entries in the table constant_poolplus one.

Why is the number indicated in this method “plus one” instead of just being equal to the number of constants? Meanwhile interfaces_count, fields_count, methods_countand attributes_countit does not seem to follow this pattern.

Some assumptions:

  • This may have something to do with achieving optimal byte alignment.
  • Perhaps this is an artifact of a particular design decision elsewhere in the JVM

: :

- , , , . -, 1 ( 0), . [3] , ( ) , phantom, .

, , long double, , " " - 1, .

+4
1

constant_pool_count constaint_pool ( ). constaint_pool [0] == constant_pool_count.

constant_pool_count - ClassFile, _ , .

__count constant_pool, constant_pool constant_pool_count - 1.

: , constant_pool_count - u2, _ cp_info (u1 + u1 = u2), u2 cp_info - .

u2 {u1; u1 [1]}, cp_info {u1; 1 []}.

0

Source: https://habr.com/ru/post/1540662/


All Articles