By the way, is this line thread-safe?
What if two threads happen to get this line while next_memtype equals to 1022, then they both return true.
After that ,memtypes may overflow.

Is that right?